thread.cc revision accd83d1523545ac69bafd38e72a7d5cff8e2fac
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> 21b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers#include <sys/mman.h> 22a09576416788b916095739e43a16917e7948f3a4Elliott Hughes 23b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro#include <algorithm> 24dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes#include <bitset> 25eb4f614f2eb53b92ebd416fa418f550861655887Elliott Hughes#include <cerrno> 26a09576416788b916095739e43a16917e7948f3a4Elliott Hughes#include <iostream> 27b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro#include <list> 28b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 29a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes#include "class_linker.h" 30df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom#include "class_loader.h" 31bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers#include "context.h" 32d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers#include "dex_verifier.h" 33408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers#include "heap.h" 34c5f7c91ab89055cffb573fff7e06dbdd860bccedElliott Hughes#include "jni_internal.h" 358e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes#include "monitor.h" 36a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes#include "object.h" 37578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "runtime.h" 385433072f589b61413e042eddf76e8190a048f71dbuzbee#include "runtime_support.h" 39726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes#include "ScopedLocalRef.h" 40aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#include "scoped_jni_thread_state.h" 4168e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack.h" 4268e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack_indirect_reference_table.h" 438daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "thread_list.h" 44a09576416788b916095739e43a16917e7948f3a4Elliott Hughes#include "utils.h" 45b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 46b557353b22c728eecbd1c68593b482622c7782a8Carl Shapironamespace art { 47b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 48b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiropthread_key_t Thread::pthread_key_self_; 49b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 508e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesstatic Class* gThreadLock = NULL; 5129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Class* gThrowable = NULL; 52038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_daemon = NULL; 53038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_group = NULL; 54038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_lock = NULL; 55038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_name = NULL; 56038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_priority = NULL; 5729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Field* gThread_uncaughtHandler = NULL; 58038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_vmData = NULL; 59038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThreadGroup_name = NULL; 608e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesstatic Field* gThreadLock_thread = NULL; 61038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Method* gThread_run = NULL; 6229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Method* gThreadGroup_removeThread = NULL; 6329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Method* gUncaughtExceptionHandler_uncaughtException = NULL; 64038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 653ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbeevoid Thread::InitFunctionPointers() { 665433072f589b61413e042eddf76e8190a048f71dbuzbee#if defined(__arm__) 675433072f589b61413e042eddf76e8190a048f71dbuzbee pShlLong = art_shl_long; 685433072f589b61413e042eddf76e8190a048f71dbuzbee pShrLong = art_shr_long; 695433072f589b61413e042eddf76e8190a048f71dbuzbee pUshrLong = art_ushr_long; 707b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pIdiv = __aeabi_idiv; 717b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pIdivmod = __aeabi_idivmod; 727b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pI2f = __aeabi_i2f; 737b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pF2iz = __aeabi_f2iz; 747b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pD2f = __aeabi_d2f; 757b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pF2d = __aeabi_f2d; 767b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pD2iz = __aeabi_d2iz; 777b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pL2f = __aeabi_l2f; 787b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pL2d = __aeabi_l2d; 797b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFadd = __aeabi_fadd; 807b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFsub = __aeabi_fsub; 817b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFdiv = __aeabi_fdiv; 827b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFmul = __aeabi_fmul; 837b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFmodf = fmodf; 847b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pDadd = __aeabi_dadd; 857b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pDsub = __aeabi_dsub; 867b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pDdiv = __aeabi_ddiv; 877b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pDmul = __aeabi_dmul; 887b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFmod = fmod; 897b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pLdivmod = __aeabi_ldivmod; 90439c4fa0db980fb19e4a585723a64a3461e4c278buzbee pLmul = __aeabi_lmul; 914a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pTestSuspendFromCode = art_test_suspend; 9221d9e8323124a832a21679ca83808bc9c68ed365Ian Rogers pAllocObjectFromCode = art_alloc_object_from_code; 93b408de744566a5c5a80be1ba7f5c88407e816945Elliott Hughes pAllocArrayFromCode = art_alloc_array_from_code; 94e51a511ccee3f3c0120807321bcc160fcaa664beIan Rogers pCanPutArrayElementFromCode = art_can_put_array_element_from_code; 95b408de744566a5c5a80be1ba7f5c88407e816945Elliott Hughes pCheckAndAllocArrayFromCode = art_check_and_alloc_array_from_code; 96ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pCheckCastFromCode = art_check_cast_from_code; 97ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pFindInstanceFieldFromCode = art_find_instance_field_from_code; 98ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pGet32Static = art_get32_static_from_code; 99ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pGet64Static = art_get64_static_from_code; 100ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pGetObjStatic = art_get_obj_static_from_code; 101ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pHandleFillArrayDataFromCode = art_handle_fill_data_from_code; 102cbba6ac9bf9a6c630a7aafae6d8767b5ddbb6fd5Ian Rogers pInitializeStaticStorage = art_initialize_static_storage_from_code; 103aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom pResolveStringFromCode = art_resolve_string_from_code; 1044a3164faefd255b1c1e911e7ad7c3d57749caaf6buzbee pInvokeInterfaceTrampoline = art_invoke_interface_trampoline; 105ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pSet32Static = art_set32_static_from_code; 106ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pSet64Static = art_set64_static_from_code; 107ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pSetObjStatic = art_set_obj_static_from_code; 108ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pThrowArrayBoundsFromCode = art_throw_array_bounds_from_code; 109ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pThrowDivZeroFromCode = art_throw_div_zero_from_code; 110c0c8dc8e677957d167066a2d06973126f108a6aaIan Rogers pThrowNegArraySizeFromCode = art_throw_neg_array_size_from_code; 111c0c8dc8e677957d167066a2d06973126f108a6aaIan Rogers pThrowNoSuchMethodFromCode = art_throw_no_such_method_from_code; 112ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pThrowNullPointerFromCode = art_throw_null_pointer_exception_from_code; 113932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers pThrowStackOverflowFromCode = art_throw_stack_overflow_from_code; 114c0c8dc8e677957d167066a2d06973126f108a6aaIan Rogers pThrowVerificationErrorFromCode = art_throw_verification_error_from_code; 1154f0d07c783afef89703dce32c94440fc8621a29bIan Rogers pLockObjectFromCode = art_lock_object_from_code; 116ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pUnlockObjectFromCode = art_unlock_object_from_code; 11767375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers#endif 118c396efc1baec875160afe34a3e65c46cce2bd72fbuzbee pF2l = F2L; 119c396efc1baec875160afe34a3e65c46cce2bd72fbuzbee pD2l = D2L; 1203ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee pMemcpy = memcpy; 1214a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pCheckSuspendFromCode = CheckSuspendFromCode; 1224a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pDebugMe = DebugMe; 1234a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pDecodeJObjectInThread = DecodeJObjectInThread; 1244a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pDeliverException = art_deliver_exception_from_code; 1254a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pFindNativeMethod = FindNativeMethod; 1261b4c85959b3d9a4a33bc2160c46c1bbde67350c7buzbee pInitializeTypeFromCode = InitializeTypeFromCode; 127caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers pInstanceofNonTrivialFromCode = IsAssignableFromCode; 12899f272349671e14ceada1cc795ce4c66a38ddd3ebuzbee pObjectInit = ObjectInitFromCode; 1294a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pResolveMethodFromCode = ResolveMethodFromCode; 1304a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pThrowAbstractMethodErrorFromCode = ThrowAbstractMethodErrorFromCode; 1314a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pUnresolvedDirectMethodTrampolineFromCode = UnresolvedDirectMethodTrampolineFromCode; 1323ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee} 1333ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee 134caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstromvoid Thread::InitTid() { 135caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom tid_ = ::art::GetTid(); 136caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom} 137caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom 138caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstromvoid Thread::InitAfterFork() { 139caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom InitTid(); 1403147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes#if defined(__BIONIC__) 1413147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes // Work around a bionic bug. 1423147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes struct bionic_pthread_internal_t { 1433147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes void* next; 1443147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes void** pref; 1453147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes pthread_attr_t attr; 1463147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes pid_t kernel_id; 1473147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes // et cetera. we just need 'kernel_id' so we can stop here. 1483147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes }; 1493147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes reinterpret_cast<bionic_pthread_internal_t*>(pthread_self())->kernel_id = tid_; 1503147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes#endif 151caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom} 152caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom 15378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromvoid* Thread::CreateCallback(void* arg) { 15493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* self = reinterpret_cast<Thread*>(arg); 15593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Runtime* runtime = Runtime::Current(); 15693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 15793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes self->Attach(runtime); 15893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 159038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes String* thread_name = reinterpret_cast<String*>(gThread_name->GetObject(self->peer_)); 16093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes if (thread_name != NULL) { 16193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes SetThreadName(thread_name->ToModifiedUtf8().c_str()); 16293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes } 16393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 16493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Wait until it's safe to start running code. (There may have been a suspend-all 16593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // in progress while we were starting up.) 16693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes runtime->GetThreadList()->WaitForGo(); 16793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 16893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // TODO: say "hi" to the debugger. 16993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes //if (gDvm.debuggerConnected) { 17093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // dvmDbgPostThreadStart(self); 17193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes //} 17293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 17393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Invoke the 'run' method of our java.lang.Thread. 17493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes CHECK(self->peer_ != NULL); 17593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Object* receiver = self->peer_; 176038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes Method* m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(gThread_run); 17793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes m->Invoke(self, receiver, NULL, NULL); 17893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 17993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Detach. 18093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes runtime->GetThreadList()->Unregister(); 18193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 182b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro return NULL; 183b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 184b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 18593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughesvoid SetVmData(Object* managed_thread, Thread* native_thread) { 186038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes gThread_vmData->SetInt(managed_thread, reinterpret_cast<uintptr_t>(native_thread)); 18793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 18893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 18901158d7a57c8321370667a6045220237d16e0da8Elliott HughesThread* Thread::FromManagedThread(JNIEnv* env, jobject java_thread) { 19001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes Object* thread = Decode<Object*>(env, java_thread); 19101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes return reinterpret_cast<Thread*>(static_cast<uintptr_t>(gThread_vmData->GetInt(thread))); 19201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes} 19301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 1947502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughessize_t FixStackSize(size_t stack_size) { 1957502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // A stack size of zero means "use the default". 196d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (stack_size == 0) { 197d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes stack_size = Runtime::Current()->GetDefaultStackSize(); 198d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 19961e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 2007502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // It's not possible to request a stack smaller than the system-defined PTHREAD_STACK_MIN. 2017502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes if (stack_size < PTHREAD_STACK_MIN) { 2027502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = PTHREAD_STACK_MIN; 2037502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes } 2047502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2057502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // It's likely that callers are trying to ensure they have at least a certain amount of 2067502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // stack space, so we should add our reserved space on top of what they requested, rather 2077502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // than implicitly take it away from them. 2087502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size += Thread::kStackOverflowReservedBytes; 2097502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2107502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // Some systems require the stack size to be a multiple of the system page size, so round up. 2117502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = RoundUp(stack_size, kPageSize); 2127502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2137502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes return stack_size; 2147502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes} 2157502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2167502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughesvoid Thread::Create(Object* peer, size_t stack_size) { 2177502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes CHECK(peer != NULL); 2187502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2197502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = FixStackSize(stack_size); 2207502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 22193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* native_thread = new Thread; 22293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes native_thread->peer_ = peer; 22393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 22493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Thread.start is synchronized, so we know that vmData is 0, 22593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // and know that we're not racing to assign it. 22693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes SetVmData(peer, native_thread); 22761e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 2283147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes pthread_t new_pthread; 22961e019d291583029c01b61b93bea750f2b663c37Carl Shapiro pthread_attr_t attr; 2308d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread"); 2318d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), "PTHREAD_CREATE_DETACHED"); 2328d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size); 2333147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes CHECK_PTHREAD_CALL(pthread_create, (&new_pthread, &attr, Thread::CreateCallback, native_thread), "new thread"); 2348d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread"); 23561e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 23693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Let the child know when it's safe to start running. 23793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Runtime::Current()->GetThreadList()->SignalGo(native_thread); 23893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 23961e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 24093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughesvoid Thread::Attach(const Runtime* runtime) { 24193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitCpu(); 24293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitFunctionPointers(); 24361e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 24493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes thin_lock_id_ = Runtime::Current()->GetThreadList()->AllocThreadId(); 245be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 246caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom InitTid(); 24793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitStackHwm(); 248dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 2493147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach"); 250a5780dad67556297c8ca5f2608c53b193e6c4514Elliott Hughes 25193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes jni_env_ = new JNIEnvExt(this, runtime->GetJavaVM()); 2525fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 2537a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes runtime->GetThreadList()->Register(); 25493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 25593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 25693e74e8d879270071c3aa163f8495ada8d21f42fElliott HughesThread* Thread::Attach(const Runtime* runtime, const char* name, bool as_daemon) { 25793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* self = new Thread; 25893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes self->Attach(runtime); 25993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 2607a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes self->SetState(Thread::kNative); 26193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 26293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes SetThreadName(name); 2635fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 2645fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes // If we're the main thread, ClassLinker won't be created until after we're attached, 2655fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes // so that thread needs a two-stage attach. Regular threads don't need this hack. 2665fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes if (self->thin_lock_id_ != ThreadList::kMainId) { 2675fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes self->CreatePeer(name, as_daemon); 2685fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes } 2695fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 270726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes self->GetJniEnv()->locals.AssertEmpty(); 271726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes 2725fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes return self; 2735fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes} 2745fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 275d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughesjobject GetWellKnownThreadGroup(JNIEnv* env, const char* field_name) { 276726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jclass> thread_group_class(env, env->FindClass("java/lang/ThreadGroup")); 277726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes jfieldID fid = env->GetStaticFieldID(thread_group_class.get(), field_name, "Ljava/lang/ThreadGroup;"); 278726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes return env->GetStaticObjectField(thread_group_class.get(), fid); 279d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes} 280d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 2815fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughesvoid Thread::CreatePeer(const char* name, bool as_daemon) { 2825fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes JNIEnv* env = jni_env_; 2835fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 284d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes const char* field_name = (GetThinLockId() == ThreadList::kMainId) ? "mMain" : "mSystem"; 285726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jobject> thread_group(env, GetWellKnownThreadGroup(env, field_name)); 286726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF(name)); 2878daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes jint thread_priority = GetNativePriority(); 2885fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes jboolean thread_is_daemon = as_daemon; 2895fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 290726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Thread")); 291726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes jmethodID mid = env->GetMethodID(c.get(), "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V"); 292330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes 293726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jobject> peer(env, 294726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes env->NewObject(c.get(), mid, thread_group.get(), thread_name.get(), thread_priority, thread_is_daemon)); 295726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes peer_ = DecodeJObject(peer.get()); 2967a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes SetVmData(peer_, Thread::Current()); 297d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 298d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes // Because we mostly run without code available (in the compiler, in tests), we 299d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes // manually assign the fields the constructor should have set. 300d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes // TODO: lose this. 30101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes gThread_daemon->SetBoolean(peer_, thread_is_daemon); 302726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes gThread_group->SetObject(peer_, Decode<Object*>(env, thread_group.get())); 303726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes gThread_name->SetObject(peer_, Decode<Object*>(env, thread_name.get())); 30401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes gThread_priority->SetInt(peer_, thread_priority); 305726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes 306726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes // Pre-allocate an OutOfMemoryError for the double-OOME case. 307726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ThrowNewException("Ljava/lang/OutOfMemoryError;", 308726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes "OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available"); 309726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jthrowable> exception(env, env->ExceptionOccurred()); 310726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes env->ExceptionClear(); 311726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes pre_allocated_OutOfMemoryError_ = Decode<Throwable*>(env, exception.get()); 31261e019d291583029c01b61b93bea750f2b663c37Carl Shapiro} 31361e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 314be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::InitStackHwm() { 315be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes pthread_attr_t attributes; 3163147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes CHECK_PTHREAD_CALL(pthread_getattr_np, (pthread_self(), &attributes), __FUNCTION__); 317be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 318932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers void* temp_stack_base; 319932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, &temp_stack_base, &stack_size_), 320932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers __FUNCTION__); 321932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers stack_base_ = reinterpret_cast<byte*>(temp_stack_base); 322be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 323932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers if (stack_size_ <= kStackOverflowReservedBytes) { 324932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers LOG(FATAL) << "attempt to attach a thread with a too-small stack (" << stack_size_ << " bytes)"; 325be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 326449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes 327932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // Set stack_end_ to the bottom of the stack saving space of stack overflows 328932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers ResetDefaultStackEnd(); 329449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes 330449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes // Sanity check. 331449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes int stack_variable; 332449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes CHECK_GT(&stack_variable, (void*) stack_end_); 333be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 3348d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); 335be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes} 336be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 337a09576416788b916095739e43a16917e7948f3a4Elliott Hughesvoid Thread::Dump(std::ostream& os) const { 338d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes DumpState(os); 339d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes DumpStack(os); 340d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes} 341d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 342d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesstd::string GetSchedulerGroup(pid_t tid) { 343d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // /proc/<pid>/group looks like this: 344d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // 2:devices:/ 345d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // 1:cpuacct,cpu:/ 346d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // We want the third field from the line whose second field contains the "cpu" token. 347d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string cgroup_file; 348d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (!ReadFileToString("/proc/self/cgroup", &cgroup_file)) { 349d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes return ""; 350d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 351d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::vector<std::string> cgroup_lines; 352d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes Split(cgroup_file, '\n', cgroup_lines); 353d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes for (size_t i = 0; i < cgroup_lines.size(); ++i) { 354d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::vector<std::string> cgroup_fields; 355d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes Split(cgroup_lines[i], ':', cgroup_fields); 356d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::vector<std::string> cgroups; 357d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes Split(cgroup_fields[1], ',', cgroups); 358d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes for (size_t i = 0; i < cgroups.size(); ++i) { 359d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (cgroups[i] == "cpu") { 360d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes return cgroup_fields[2].substr(1); // Skip the leading slash. 361d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 362d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 363d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 364d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes return ""; 365d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes} 366d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 367d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesvoid Thread::DumpState(std::ostream& os) const { 368d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::string thread_name("<native thread without managed peer>"); 369d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::string group_name; 370d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes int priority; 371d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes bool is_daemon = false; 372d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 373d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (peer_ != NULL) { 374038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes String* thread_name_string = reinterpret_cast<String*>(gThread_name->GetObject(peer_)); 375d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes thread_name = (thread_name_string != NULL) ? thread_name_string->ToModifiedUtf8() : "<null>"; 376038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes priority = gThread_priority->GetInt(peer_); 377038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes is_daemon = gThread_daemon->GetBoolean(peer_); 378d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 379038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes Object* thread_group = gThread_group->GetObject(peer_); 380d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (thread_group != NULL) { 381038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes String* group_name_string = reinterpret_cast<String*>(gThreadGroup_name->GetObject(thread_group)); 382d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes group_name = (group_name_string != NULL) ? group_name_string->ToModifiedUtf8() : "<null>"; 383d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 384d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } else { 385d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes // This name may be truncated, but it's the best we can do in the absence of a managed peer. 386dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes std::string stats; 387dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes if (ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { 388dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes size_t start = stats.find('(') + 1; 389dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes size_t end = stats.find(')') - start; 390dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes thread_name = stats.substr(start, end); 391dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes } 392d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes priority = GetNativePriority(); 393dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes } 394d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 395d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int policy; 396d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes sched_param sp; 3973147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes CHECK_PTHREAD_CALL(pthread_getschedparam, (pthread_self(), &policy, &sp), __FUNCTION__); 398d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 399d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string scheduler_group(GetSchedulerGroup(GetTid())); 400d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (scheduler_group.empty()) { 401d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_group = "default"; 402d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 403d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 404d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << '"' << thread_name << '"'; 405d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (is_daemon) { 406d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " daemon"; 407d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 408d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " prio=" << priority 409dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes << " tid=" << GetThinLockId() 41093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes << " " << GetState() << "\n"; 411d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 412d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int debug_suspend_count = 0; // TODO 413d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | group=\"" << group_name << "\"" 4148d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes << " sCount=" << suspend_count_ 415d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " dsCount=" << debug_suspend_count 416dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes << " obj=" << reinterpret_cast<void*>(peer_) 417d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " self=" << reinterpret_cast<const void*>(this) << "\n"; 418d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | sysTid=" << GetTid() 419d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " nice=" << getpriority(PRIO_PROCESS, GetTid()) 420d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " sched=" << policy << "/" << sp.sched_priority 421d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " cgrp=" << scheduler_group 4223147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes << " handle=" << pthread_self() << "\n"; 423d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 424d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // Grab the scheduler stats for this thread. 425d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string scheduler_stats; 426d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", GetTid()).c_str(), &scheduler_stats)) { 427d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'. 428d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } else { 429d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_stats = "0 0 0"; 430d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 431d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 432d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int utime = 0; 433d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int stime = 0; 434d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int task_cpu = 0; 435d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string stats; 436d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { 437d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // Skip the command, which may contain spaces. 438d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes stats = stats.substr(stats.find(')') + 2); 439d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // Extract the three fields we care about. 440d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::vector<std::string> fields; 441d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes Split(stats, ' ', fields); 442d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes utime = strtoull(fields[11].c_str(), NULL, 10); 443d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes stime = strtoull(fields[12].c_str(), NULL, 10); 444d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes task_cpu = strtoull(fields[36].c_str(), NULL, 10); 445d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 446d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 447d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | schedstat=( " << scheduler_stats << " )" 448d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " utm=" << utime 449d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " stm=" << stime 450d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " core=" << task_cpu 451d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " HZ=" << sysconf(_SC_CLK_TCK) << "\n"; 452a09576416788b916095739e43a16917e7948f3a4Elliott Hughes} 453a09576416788b916095739e43a16917e7948f3a4Elliott Hughes 454b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogersvoid Thread::PushNativeToManagedRecord(NativeToManagedRecord* record) { 455b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers Method **sp = top_of_managed_stack_.GetSP(); 456b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers#ifndef NDEBUG 457b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers if (sp != NULL) { 458b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers Method* m = *sp; 459b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers Heap::VerifyObject(m); 460b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers DCHECK((m == NULL) || m->IsMethod()); 461b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers } 462b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers#endif 463b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers record->last_top_of_managed_stack_ = reinterpret_cast<void*>(sp); 464b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers record->last_top_of_managed_stack_pc_ = top_of_managed_stack_pc_; 465b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers record->link_ = native_to_managed_record_; 466b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers native_to_managed_record_ = record; 467b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers top_of_managed_stack_.SetSP(NULL); 468b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers} 469b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers 470b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogersvoid Thread::PopNativeToManagedRecord(const NativeToManagedRecord& record) { 471b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers native_to_managed_record_ = record.link_; 472b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers top_of_managed_stack_.SetSP(reinterpret_cast<Method**>(record.last_top_of_managed_stack_)); 473b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers top_of_managed_stack_pc_ = record.last_top_of_managed_stack_pc_; 474b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers} 475b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers 476d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughesstruct StackDumpVisitor : public Thread::StackVisitor { 4778e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes StackDumpVisitor(std::ostream& os, const Thread* thread) 4788e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes : os(os), thread(thread), frame_count(0) { 479d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 480d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 481bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers virtual ~StackDumpVisitor() { 482d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 483d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 484bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers void VisitFrame(const Frame& frame, uintptr_t pc) { 4859086572fa809d1a19d886b467e4da3ce42016982Ian Rogers if (!frame.HasMethod()) { 4869086572fa809d1a19d886b467e4da3ce42016982Ian Rogers return; 4879086572fa809d1a19d886b467e4da3ce42016982Ian Rogers } 488d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 489d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Method* m = frame.GetMethod(); 490d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Class* c = m->GetDeclaringClass(); 4918e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 492d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes const DexFile& dex_file = class_linker->FindDexFile(c->GetDexCache()); 493d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 494d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes os << " at " << PrettyMethod(m, false); 495d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (m->IsNative()) { 496d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes os << "(Native method)"; 497d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } else { 498bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers int line_number = dex_file.GetLineNumFromPC(m, m->ToDexPC(pc)); 499d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes os << "(" << c->GetSourceFile()->ToModifiedUtf8() << ":" << line_number << ")"; 500d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 501d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes os << "\n"; 5028e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 5038e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (frame_count++ == 0) { 5048e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Monitor::DescribeWait(os, thread); 5058e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 506d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 507d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 508d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::ostream& os; 5098e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes const Thread* thread; 5108e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes int frame_count; 511d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes}; 512d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 513d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesvoid Thread::DumpStack(std::ostream& os) const { 5148e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes StackDumpVisitor dumper(os, this); 515d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes WalkStack(&dumper); 516e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes} 517e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes 5188d768a954b101a9532f980253ac46be2c53aba11Elliott HughesThread::State Thread::SetState(Thread::State new_state) { 5198d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes Thread::State old_state = state_; 5208d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (old_state == new_state) { 5218d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes return old_state; 5228d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 5238d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 5248d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes volatile void* raw = reinterpret_cast<volatile void*>(&state_); 5258d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes volatile int32_t* addr = reinterpret_cast<volatile int32_t*>(raw); 5268d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 5278d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (new_state == Thread::kRunnable) { 5288d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes /* 5298d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Change our status to Thread::kRunnable. The transition requires 5308d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * that we check for pending suspension, because the VM considers 5318d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * us to be "asleep" in all other states, and another thread could 5328d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * be performing a GC now. 5338d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5348d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * The order of operations is very significant here. One way to 5358d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * do this wrong is: 5368d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5378d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * GCing thread Our thread (in kNative) 5388d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * ------------ ---------------------- 5398d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * check suspend count (== 0) 5408d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * SuspendAllThreads() 5418d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * grab suspend-count lock 5428d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * increment all suspend counts 5438d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * release suspend-count lock 5448d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * check thread state (== kNative) 5458d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * all are suspended, begin GC 5468d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * set state to kRunnable 5478d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * (continue executing) 5488d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5498d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We can correct this by grabbing the suspend-count lock and 5508d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * performing both of our operations (check suspend count, set 5518d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * state) while holding it, now we need to grab a mutex on every 5528d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * transition to kRunnable. 5538d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5548d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * What we do instead is change the order of operations so that 5558d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * the transition to kRunnable happens first. If we then detect 5568d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * that the suspend count is nonzero, we switch to kSuspended. 5578d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5588d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Appropriate compiler and memory barriers are required to ensure 5598d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * that the operations are observed in the expected order. 5608d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5618d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * This does create a small window of opportunity where a GC in 5628d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * progress could observe what appears to be a running thread (if 5638d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * it happens to look between when we set to kRunnable and when we 5648d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * switch to kSuspended). At worst this only affects assertions 5658d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * and thread logging. (We could work around it with some sort 5668d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * of intermediate "pre-running" state that is generally treated 5678d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * as equivalent to running, but that doesn't seem worthwhile.) 5688d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5698d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We can also solve this by combining the "status" and "suspend 5708d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * count" fields into a single 32-bit value. This trades the 5718d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * store/load barrier on transition to kRunnable for an atomic RMW 5728d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * op on all transitions and all suspend count updates (also, all 5738d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * accesses to status or the thread count require bit-fiddling). 5748d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * It also eliminates the brief transition through kRunnable when 5758d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * the thread is supposed to be suspended. This is possibly faster 5768d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * on SMP and slightly more correct, but less convenient. 5778d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes */ 5788d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes android_atomic_acquire_store(new_state, addr); 5798d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (ANNOTATE_UNPROTECTED_READ(suspend_count_) != 0) { 5808d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes Runtime::Current()->GetThreadList()->FullSuspendCheck(this); 5818d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 5828d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } else { 5838d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes /* 5848d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Not changing to Thread::kRunnable. No additional work required. 5858d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5868d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We use a releasing store to ensure that, if we were runnable, 5878d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * any updates we previously made to objects on the managed heap 5888d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * will be observed before the state change. 5898d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes */ 5908d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes android_atomic_release_store(new_state, addr); 5918d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 5928d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 5938d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes return old_state; 5948d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes} 5958d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 5968d768a954b101a9532f980253ac46be2c53aba11Elliott Hughesvoid Thread::WaitUntilSuspended() { 5978d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes // TODO: dalvik dropped the waiting thread's priority after a while. 5988d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes // TODO: dalvik timed out and aborted. 5998d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes useconds_t delay = 0; 6008d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes while (GetState() == Thread::kRunnable) { 6018d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes useconds_t new_delay = delay * 2; 6028d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_GE(new_delay, delay); 6038d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes delay = new_delay; 6048d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (delay == 0) { 6058d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes sched_yield(); 6068d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes delay = 10000; 6078d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } else { 6088d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes usleep(delay); 6098d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 6108d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 6118d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes} 6128d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 613be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::ThreadExitCallback(void* arg) { 614be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes Thread* self = reinterpret_cast<Thread*>(arg); 615be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self; 616b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 617b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 618be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::Startup() { 619b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro // Allocate a TLS slot. 6208d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_key_create, (&Thread::pthread_key_self_, Thread::ThreadExitCallback), "self key"); 621b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 622b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro // Double-check the TLS slot allocation. 623b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro if (pthread_getspecific(pthread_key_self_) != NULL) { 624be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes LOG(FATAL) << "newly-created pthread TLS slot is not NULL"; 625b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro } 626038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes} 627038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 6288e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 6298e4aac52962d54cb4be2078b9cd95685e067133aElliott HughesClass* FindPrimitiveClassOrDie(ClassLinker* class_linker, char descriptor) { 6308e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* c = class_linker->FindPrimitiveClass(descriptor); 6318e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(c != NULL) << descriptor; 6328e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return c; 6338e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 6348e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6358e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 6368e4aac52962d54cb4be2078b9cd95685e067133aElliott HughesClass* FindClassOrDie(ClassLinker* class_linker, const char* descriptor) { 6378e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* c = class_linker->FindSystemClass(descriptor); 6388e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(c != NULL) << descriptor; 6398e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return c; 6408e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 6418e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6428e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 6438e4aac52962d54cb4be2078b9cd95685e067133aElliott HughesField* FindFieldOrDie(Class* c, const char* name, Class* type) { 6448e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Field* f = c->FindDeclaredInstanceField(name, type); 6458e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(f != NULL) << PrettyClass(c) << " " << name << " " << PrettyClass(type); 6468e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return f; 6478e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 6488e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6498e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 6508e4aac52962d54cb4be2078b9cd95685e067133aElliott HughesMethod* FindMethodOrDie(Class* c, const char* name, const char* signature) { 6518e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Method* m = c->FindVirtualMethod(name, signature); 6528e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(m != NULL) << PrettyClass(c) << " " << name << " " << signature; 6538e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return m; 6548e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 6558e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 656038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesvoid Thread::FinishStartup() { 657038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes // Now the ClassLinker is ready, we can find the various Class*, Field*, and Method*s we need. 658038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 6598e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6608e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* boolean_class = FindPrimitiveClassOrDie(class_linker, 'Z'); 6618e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* int_class = FindPrimitiveClassOrDie(class_linker, 'I'); 662df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom Class* ClassLoader_class = FindClassOrDie(class_linker, "Ljava/lang/ClassLoader;"); 6638e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* String_class = FindClassOrDie(class_linker, "Ljava/lang/String;"); 6648e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* Thread_class = FindClassOrDie(class_linker, "Ljava/lang/Thread;"); 6658e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* ThreadGroup_class = FindClassOrDie(class_linker, "Ljava/lang/ThreadGroup;"); 6668e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* UncaughtExceptionHandler_class = FindClassOrDie(class_linker, "Ljava/lang/Thread$UncaughtExceptionHandler;"); 6678e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThreadLock = FindClassOrDie(class_linker, "Ljava/lang/ThreadLock;"); 6688e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThrowable = FindClassOrDie(class_linker, "Ljava/lang/Throwable;"); 6698e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6708e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_daemon = FindFieldOrDie(Thread_class, "daemon", boolean_class); 6718e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_group = FindFieldOrDie(Thread_class, "group", ThreadGroup_class); 6728e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_lock = FindFieldOrDie(Thread_class, "lock", gThreadLock); 6738e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_name = FindFieldOrDie(Thread_class, "name", String_class); 6748e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_priority = FindFieldOrDie(Thread_class, "priority", int_class); 6758e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_uncaughtHandler = FindFieldOrDie(Thread_class, "uncaughtHandler", UncaughtExceptionHandler_class); 6768e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_vmData = FindFieldOrDie(Thread_class, "vmData", int_class); 6778e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThreadGroup_name = FindFieldOrDie(ThreadGroup_class, "name", String_class); 6788e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThreadLock_thread = FindFieldOrDie(gThreadLock, "thread", Thread_class); 6798e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6808e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_run = FindMethodOrDie(Thread_class, "run", "()V"); 6818e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThreadGroup_removeThread = FindMethodOrDie(ThreadGroup_class, "removeThread", "(Ljava/lang/Thread;)V"); 6828e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gUncaughtExceptionHandler_uncaughtException = FindMethodOrDie(UncaughtExceptionHandler_class, 6838e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes "uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V"); 68401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 68501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Finish attaching the main thread. 686df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom Thread* self = Thread::Current(); 687df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom self->CreatePeer("main", false); 688df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom 689df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom const Field* Thread_contextClassLoader = FindFieldOrDie(Thread_class , "contextClassLoader", ClassLoader_class); 690df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom Thread_contextClassLoader->SetObject(self->GetPeer(), self->GetClassLoaderOverride()); 691b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 692b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 693c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughesvoid Thread::Shutdown() { 6948d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key"); 695c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes} 696c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes 6978e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesuint32_t Thread::LockOwnerFromThreadLock(Object* thread_lock) { 6988e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (thread_lock == NULL || thread_lock->GetClass() != gThreadLock) { 6998e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 7008e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 7018e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Object* managed_thread = gThreadLock_thread->GetObject(thread_lock); 7028e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (managed_thread == NULL) { 7038e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 7048e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 7058e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes uintptr_t vmData = static_cast<uintptr_t>(gThread_vmData->GetInt(managed_thread)); 7068e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Thread* thread = reinterpret_cast<Thread*>(vmData); 7078e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (thread == NULL) { 7088e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 7098e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 7108e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return thread->GetThinLockId(); 7118e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 7128e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 713dcc247493fd8fb243e335c3ec08e5e625896a47cElliott HughesThread::Thread() 71402b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes : peer_(NULL), 7158e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes top_of_managed_stack_(), 7168e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes top_of_managed_stack_pc_(0), 71785d1545e985ac689db4bad7849880e843707c862Elliott Hughes wait_mutex_(new Mutex("Thread wait mutex")), 71885d1545e985ac689db4bad7849880e843707c862Elliott Hughes wait_cond_(new ConditionVariable("Thread wait condition variable")), 7198daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes wait_monitor_(NULL), 7208daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes interrupted_(false), 721dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes wait_next_(NULL), 7228e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes monitor_enter_object_(NULL), 723dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes card_table_(0), 7248daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes stack_end_(NULL), 725dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes native_to_managed_record_(NULL), 726dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes top_sirt_(NULL), 727dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes jni_env_(NULL), 7288e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes state_(Thread::kNative), 729dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes self_(NULL), 730dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes runtime_(NULL), 731dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes exception_(NULL), 732dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes suspend_count_(0), 73385d1545e985ac689db4bad7849880e843707c862Elliott Hughes class_loader_override_(NULL), 734418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes long_jump_context_(NULL), 735726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_(false), 736726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes pre_allocated_OutOfMemoryError_(NULL) { 737f5a7a476e7ea63e094ff0f011dccc170607e6f6bElliott Hughes CHECK_EQ((sizeof(Thread) % 4), 0U) << sizeof(Thread); 738dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes} 739dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 74002b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughesvoid MonitorExitVisitor(const Object* object, void*) { 74102b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes Object* entered_monitor = const_cast<Object*>(object); 7425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes entered_monitor->MonitorExit(Thread::Current()); 74302b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes} 74402b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 745c1674ed06662420213441ff2b818f2f71f9098dcElliott HughesThread::~Thread() { 7467a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes SetState(Thread::kRunnable); 7477a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes 74802b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited. 74993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes if (jni_env_ != NULL) { 75093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes jni_env_->monitors.VisitRoots(MonitorExitVisitor, NULL); 75193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes } 75202b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 75329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (peer_ != NULL) { 75429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // this.group.removeThread(this); 755081be7fcd72490937e188e9ef56e72df06b7b006Elliott Hughes // group can be null if we're in the compiler or a test. 756accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Object* group = gThread_group->GetObject(peer_); 757081be7fcd72490937e188e9ef56e72df06b7b006Elliott Hughes if (group != NULL) { 758081be7fcd72490937e188e9ef56e72df06b7b006Elliott Hughes Method* m = group->GetClass()->FindVirtualMethodForVirtualOrInterface(gThreadGroup_removeThread); 759081be7fcd72490937e188e9ef56e72df06b7b006Elliott Hughes Object* args = peer_; 760081be7fcd72490937e188e9ef56e72df06b7b006Elliott Hughes m->Invoke(this, group, reinterpret_cast<byte*>(&args), NULL); 761081be7fcd72490937e188e9ef56e72df06b7b006Elliott Hughes } 76229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 76329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // this.vmData = 0; 76493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes SetVmData(peer_, NULL); 76502b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 76629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // TODO: say "bye" to the debugger. 76729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes //if (gDvm.debuggerConnected) { 76829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // dvmDbgPostThreadDeath(self); 76929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes //} 77002b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 77129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // Thread.join() is implemented as an Object.wait() on the Thread.lock 77229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // object. Signal anyone who is waiting. 7735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* self = Thread::Current(); 774038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes Object* lock = gThread_lock->GetObject(peer_); 775038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes // (This conditional is only needed for tests, where Thread.lock won't have been set.) 7765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (lock != NULL) { 7775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->MonitorEnter(self); 7785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->NotifyAll(); 7795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->MonitorExit(self); 7805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 78202b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 783c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes delete jni_env_; 78402b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes jni_env_ = NULL; 78502b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 78602b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes SetState(Thread::kTerminated); 78785d1545e985ac689db4bad7849880e843707c862Elliott Hughes 78885d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete wait_cond_; 78985d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete wait_mutex_; 79085d1545e985ac689db4bad7849880e843707c862Elliott Hughes 79185d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete long_jump_context_; 792c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes} 793c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes 794accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughesvoid Thread::HandleUncaughtExceptions() { 795accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes if (!IsExceptionPending()) { 796accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes return; 797accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes } 798accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 799accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes ScopedThreadStateChange tsc(this, Thread::kRunnable); 800accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 801accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Get and clear the exception. 802accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Object* exception = GetException(); 803accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes ClearException(); 804accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 805accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // If the thread has its own handler, use that. 806accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Object* handler = gThread_uncaughtHandler->GetObject(peer_); 807accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes if (handler == NULL) { 808accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Otherwise use the thread group's default handler. 809accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes handler = gThread_group->GetObject(peer_); 810accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes } 811accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 812accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Call the handler. 813accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Method* m = handler->GetClass()->FindVirtualMethodForVirtualOrInterface(gUncaughtExceptionHandler_uncaughtException); 814accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Object* args[2]; 815accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes args[0] = peer_; 816accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes args[1] = exception; 817accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes m->Invoke(this, handler, reinterpret_cast<byte*>(&args), NULL); 818accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 819accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // If the handler threw, clear that exception too. 820accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes ClearException(); 821accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 822accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 823408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogerssize_t Thread::NumSirtReferences() { 824a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers size_t count = 0; 825408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { 826a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers count += cur->NumberOfReferences(); 827a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 828a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return count; 829a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers} 830a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers 831408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogersbool Thread::SirtContains(jobject obj) { 832408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers Object** sirt_entry = reinterpret_cast<Object**>(obj); 833408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { 834a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers size_t num_refs = cur->NumberOfReferences(); 835408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // A SIRT should always have a jobject/jclass as a native method is passed 836408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // in a this pointer or a class 837408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers DCHECK_GT(num_refs, 0u); 8382f0ce9d60a4a9371c63a32a3764320fb02341acbShih-wei Liao if ((&cur->References()[0] <= sirt_entry) && 8392f0ce9d60a4a9371c63a32a3764320fb02341acbShih-wei Liao (sirt_entry <= (&cur->References()[num_refs - 1]))) { 840a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return true; 841a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 842a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 843a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return false; 844a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers} 845a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers 8468dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liaovoid Thread::SirtVisitRoots(Heap::RootVisitor* visitor, void* arg) { 8478dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { 8488dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao size_t num_refs = cur->NumberOfReferences(); 8498dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao for (size_t j = 0; j < num_refs; j++) { 8505e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom Object* object = cur->References()[j]; 8515e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom if (object != NULL) { 8525e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom visitor(object, arg); 8535e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom } 8548dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao } 8558dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao } 8568dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao} 8578dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 858408f79aeb676251ba35667a64e86c20638d7cb0bIan RogersObject* Thread::DecodeJObject(jobject obj) { 8590cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers DCHECK(CanAccessDirectReferences()); 860408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (obj == NULL) { 861408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return NULL; 862408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 863408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectRef ref = reinterpret_cast<IndirectRef>(obj); 864408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectRefKind kind = GetIndirectRefKind(ref); 865408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers Object* result; 866408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers switch (kind) { 867408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kLocal: 868408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 86969f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughes IndirectReferenceTable& locals = jni_env_->locals; 870cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(locals.Get(ref)); 871408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 872408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 873408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kGlobal: 874408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 875408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers JavaVMExt* vm = Runtime::Current()->GetJavaVM(); 876408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectReferenceTable& globals = vm->globals; 877408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers MutexLock mu(vm->globals_lock); 878cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(globals.Get(ref)); 879408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 880408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 881408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kWeakGlobal: 882408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 883408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers JavaVMExt* vm = Runtime::Current()->GetJavaVM(); 884408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectReferenceTable& weak_globals = vm->weak_globals; 885408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers MutexLock mu(vm->weak_globals_lock); 886cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(weak_globals.Get(ref)); 887408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (result == kClearedJniWeakGlobal) { 888408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // This is a special case where it's okay to return NULL. 889408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return NULL; 890408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 891408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 892408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 893408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kSirtOrInvalid: 894408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers default: 895408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // TODO: make stack indirect reference table lookup more efficient 896408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Check if this is a local reference in the SIRT 897408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (SirtContains(obj)) { 8980cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers result = *reinterpret_cast<Object**>(obj); // Read from SIRT 899c5bfa8f49d8548d7c685a99b411311ef56bedffaElliott Hughes } else if (jni_env_->work_around_app_jni_bugs) { 900408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Assume an invalid local reference is actually a direct pointer. 901408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers result = reinterpret_cast<Object*>(obj); 902408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } else { 903a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes result = kInvalidIndirectRefObject; 904408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 905408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 906408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 907408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (result == NULL) { 908a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes LOG(ERROR) << "JNI ERROR (app bug): use of deleted " << kind << ": " << obj; 909a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes JniAbort(NULL); 910a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes } else { 911a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes if (result != kInvalidIndirectRefObject) { 912a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes Heap::VerifyObject(result); 913a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes } 914408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 915408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return result; 916408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers} 917408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 9189b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liaoclass CountStackDepthVisitor : public Thread::StackVisitor { 9199b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 92029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes CountStackDepthVisitor() : depth_(0), skip_depth_(0), skipping_(true) {} 921d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 92229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes virtual void VisitFrame(const Frame& frame, uintptr_t pc) { 92329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // We want to skip frames up to and including the exception's constructor. 9249086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // Note we also skip the frame if it doesn't have a method (namely the callee 9259086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // save frame) 92625c3325bf95036bf325fc7cb21b4fd6d40282857Brian Carlstrom DCHECK(gThrowable != NULL); 9279086572fa809d1a19d886b467e4da3ce42016982Ian Rogers if (skipping_ && frame.HasMethod() && !gThrowable->IsAssignableFrom(frame.GetMethod()->GetDeclaringClass())) { 92829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes skipping_ = false; 92929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 93029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (!skipping_) { 93129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes ++depth_; 93229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } else { 93329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes ++skip_depth_; 93429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 9359b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9369b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 9379b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao int GetDepth() const { 938aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return depth_; 9399b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9409b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 94129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int GetSkipDepth() const { 94229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes return skip_depth_; 94329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 94429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 9459b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao private: 946aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t depth_; 94729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes uint32_t skip_depth_; 94829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes bool skipping_; 9499b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao}; 9509b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 951aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogersclass BuildInternalStackTraceVisitor : public Thread::StackVisitor { 9529b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 95329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes explicit BuildInternalStackTraceVisitor(int depth, int skip_depth, ScopedJniThreadState& ts) 954726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes : skip_depth_(skip_depth), count_(0), pc_trace_(NULL), method_trace_(NULL), local_ref_(NULL) { 955aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Allocate method trace with an extra slot that will hold the PC trace 95601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes method_trace_ = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(depth + 1); 957726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (method_trace_ == NULL) { 958726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes return; 959726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 960aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Register a local reference as IntArray::Alloc may trigger GC 961aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers local_ref_ = AddLocalReference<jobject>(ts.Env(), method_trace_); 962aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers pc_trace_ = IntArray::Alloc(depth); 963726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (pc_trace_ == NULL) { 964726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes return; 965726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 966aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#ifdef MOVING_GARBAGE_COLLECTOR 967aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Re-read after potential GC 968aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace = Decode<ObjectArray<Object>*>(ts.Env(), local_ref_); 969aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#endif 970aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Save PC trace in last element of method trace, also places it into the 971aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // object graph. 972aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace_->Set(depth, pc_trace_); 9739b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9749b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 975aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers virtual ~BuildInternalStackTraceVisitor() {} 9769b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 977bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers virtual void VisitFrame(const Frame& frame, uintptr_t pc) { 978726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (method_trace_ == NULL || pc_trace_ == NULL) { 979726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes return; // We're probably trying to fillInStackTrace for an OutOfMemoryError. 980726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 98129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (skip_depth_ > 0) { 98229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes skip_depth_--; 98329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes return; 98429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 985aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace_->Set(count_, frame.GetMethod()); 986bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers pc_trace_->Set(count_, pc); 987aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ++count_; 9889b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9899b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 990aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers jobject GetInternalStackTrace() const { 991aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return local_ref_; 9929b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9939b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 9949b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao private: 99529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // How many more frames to skip. 99629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int32_t skip_depth_; 997aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Current position down stack trace 998aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t count_; 999aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Array of return PC values 1000aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers IntArray* pc_trace_; 1001aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // An array of the methods on the stack, the last entry is a reference to the 1002aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // PC trace 1003aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ObjectArray<Object>* method_trace_; 1004aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Local indirect reference table entry for method trace 1005aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers jobject local_ref_; 10069b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao}; 10079b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1008a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes// TODO: remove this. 1009a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughesuintptr_t ManglePc(uintptr_t pc) { 1010a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // Move the PC back 2 bytes as a call will frequently terminate the 1011a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // decoding of a particular instruction and we want to make sure we 1012a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // get the Dex PC of the instruction with the call and not the 1013a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // instruction following. 1014a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes if (pc > 0) { pc -= 2; } 1015a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes return pc; 1016a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes} 1017a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes 1018a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes// TODO: remove this. 1019a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughesuintptr_t DemanglePc(uintptr_t pc) { 1020a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // Revert mangling for the case where we need the PC to return to the upcall 1021a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes return pc + 2; 1022a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes} 1023f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers 1024aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogersvoid Thread::WalkStack(StackVisitor* visitor) const { 1025d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Frame frame = GetTopOfStack(); 1026a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes uintptr_t pc = ManglePc(top_of_managed_stack_pc_); 10279b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao // TODO: enable this CHECK after native_to_managed_record_ is initialized during startup. 10289b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao // CHECK(native_to_managed_record_ != NULL); 10299b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao NativeToManagedRecord* record = native_to_managed_record_; 10304417536522fd2a9d8215d8672331984769c9520bShih-wei Liao 1031bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers while (frame.GetSP() != 0) { 10329b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao for ( ; frame.GetMethod() != 0; frame.Next()) { 10333320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom // DCHECK(frame.GetMethod()->IsWithinCode(pc)); // TODO: restore IsWithinCode 10343ddac99d4dc6a036fac59d8f0bdc664ef619fb04Shih-wei Liao visitor->VisitFrame(frame, pc); 1035a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes pc = ManglePc(frame.GetReturnPC()); 10369b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 10379b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao if (record == NULL) { 10389b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao break; 10399b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 1040bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // last_tos should return Frame instead of sp? 1041ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers frame.SetSP(reinterpret_cast<Method**>(record->last_top_of_managed_stack_)); 1042a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes pc = ManglePc(record->last_top_of_managed_stack_pc_); 1043bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers record = record->link_; 1044bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1045bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers} 1046bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 104767375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogersvoid Thread::WalkStackUntilUpCall(StackVisitor* visitor, bool include_upcall) const { 1048bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Frame frame = GetTopOfStack(); 1049a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes uintptr_t pc = ManglePc(top_of_managed_stack_pc_); 1050bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1051bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (frame.GetSP() != 0) { 1052bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers for ( ; frame.GetMethod() != 0; frame.Next()) { 10533320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom // DCHECK(frame.GetMethod()->IsWithinCode(pc)); // TODO: restore IsWithinCode 10543ddac99d4dc6a036fac59d8f0bdc664ef619fb04Shih-wei Liao visitor->VisitFrame(frame, pc); 1055a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes pc = ManglePc(frame.GetReturnPC()); 1056bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 105767375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers if (include_upcall) { 105867375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers visitor->VisitFrame(frame, pc); 105967375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers } 106055df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao } 106155df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao} 106255df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 106301158d7a57c8321370667a6045220237d16e0da8Elliott Hughesjobject Thread::CreateInternalStackTrace(JNIEnv* env) const { 1064aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Compute depth of stack 10659b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao CountStackDepthVisitor count_visitor; 10669b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao WalkStack(&count_visitor); 10679b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao int32_t depth = count_visitor.GetDepth(); 106829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int32_t skip_depth = count_visitor.GetSkipDepth(); 10694417536522fd2a9d8215d8672331984769c9520bShih-wei Liao 1070aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Transition into runnable state to work on Object*/Array* 107101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes ScopedJniThreadState ts(env); 1072aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 1073aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Build internal stack trace 107429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes BuildInternalStackTraceVisitor build_trace_visitor(depth, skip_depth, ts); 10759b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao WalkStack(&build_trace_visitor); 10764417536522fd2a9d8215d8672331984769c9520bShih-wei Liao 1077aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return build_trace_visitor.GetInternalStackTrace(); 1078aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers} 1079aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 108001158d7a57c8321370667a6045220237d16e0da8Elliott HughesjobjectArray Thread::InternalStackTraceToStackTraceElementArray(JNIEnv* env, jobject internal, 108101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobjectArray output_array, int* stack_depth) { 1082aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Transition into runnable state to work on Object*/Array* 1083aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ScopedJniThreadState ts(env); 1084aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 1085aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Decode the internal stack trace into the depth, method trace and PC trace 1086aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ObjectArray<Object>* method_trace = 1087aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); 1088aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers int32_t depth = method_trace->GetLength()-1; 1089aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); 1090aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 1091aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1092aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 109301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobjectArray result; 109401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes ObjectArray<StackTraceElement>* java_traces; 109501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes if (output_array != NULL) { 109601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Reuse the array we were given. 109701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes result = output_array; 109801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes java_traces = reinterpret_cast<ObjectArray<StackTraceElement>*>(Decode<Array*>(env, 109901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes output_array)); 110001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // ...adjusting the number of frames we'll write to not exceed the array length. 110101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes depth = std::min(depth, java_traces->GetLength()); 110201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } else { 110301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Create java_trace array and place in local reference table 110401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes java_traces = class_linker->AllocStackTraceElementArray(depth); 110530646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (java_traces == NULL) { 110630646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return NULL; 110730646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 110801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes result = AddLocalReference<jobjectArray>(ts.Env(), java_traces); 110901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } 111001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 111101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes if (stack_depth != NULL) { 111201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes *stack_depth = depth; 111301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } 111455df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 11159b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao for (int32_t i = 0; i < depth; ++i) { 1116aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Prepare parameters for StackTraceElement(String cls, String method, String file, int line) 1117aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers Method* method = down_cast<Method*>(method_trace->Get(i)); 1118aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t native_pc = pc_trace->Get(i); 1119aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers Class* klass = method->GetDeclaringClass(); 112038933579b15c74baa09b8713ee8bf715529d05ebElliott Hughes std::string class_name(PrettyDescriptor(klass->GetDescriptor())); 1121466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers int32_t line_number = -1; 1122466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers DexCache* dex_cache = klass->GetDexCache(); 1123466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers if (dex_cache != NULL) { 1124466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers const DexFile& dex_file = class_linker->FindDexFile(dex_cache); 1125466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers line_number = dex_file.GetLineNumFromPC(method, method->ToDexPC(native_pc)); 1126466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers } 1127aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Allocate element, potentially triggering GC 112855df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao StackTraceElement* obj = 112938933579b15c74baa09b8713ee8bf715529d05ebElliott Hughes StackTraceElement::Alloc(String::AllocFromModifiedUtf8(class_name.c_str()), 1130466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers method->GetName(), klass->GetSourceFile(), line_number); 113130646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (obj == NULL) { 113230646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return NULL; 113330646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1134aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#ifdef MOVING_GARBAGE_COLLECTOR 1135aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Re-read after potential GC 1136aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers java_traces = Decode<ObjectArray<Object>*>(ts.Env(), result); 1137aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace = down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); 1138aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); 1139aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#endif 114055df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao java_traces->Set(i, obj); 114155df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao } 1142aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return result; 114355df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao} 114455df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 11455cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughesvoid Thread::ThrowNewExceptionF(const char* exception_class_descriptor, const char* fmt, ...) { 1146a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_list args; 1147a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_start(args, fmt); 11484a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes ThrowNewExceptionV(exception_class_descriptor, fmt, args); 1149a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_end(args); 11504a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes} 11514a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes 11524a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughesvoid Thread::ThrowNewExceptionV(const char* exception_class_descriptor, const char* fmt, va_list ap) { 11534a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes std::string msg; 11544a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes StringAppendV(&msg, fmt, ap); 11555cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes ThrowNewException(exception_class_descriptor, msg.c_str()); 11565cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes} 115737f7a40f6789bb287f287a9af00777af9d6428eeElliott Hughes 11585cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughesvoid Thread::ThrowNewException(const char* exception_class_descriptor, const char* msg) { 1159e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes // Convert "Ljava/lang/Exception;" into JNI-style "java/lang/Exception". 11600cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers CHECK_EQ('L', exception_class_descriptor[0]); 1161e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes std::string descriptor(exception_class_descriptor + 1); 11620cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers CHECK_EQ(';', descriptor[descriptor.length() - 1]); 1163e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes descriptor.erase(descriptor.length() - 1); 1164e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes 1165e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes JNIEnv* env = GetJniEnv(); 1166726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jclass> exception_class(env, env->FindClass(descriptor.c_str())); 116730646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (exception_class.get() == NULL) { 116830646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI FindClass failed: " 116930646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes << PrettyTypeOf(GetException()); 117030646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes CHECK(IsExceptionPending()); 117130646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return; 117230646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1173726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes int rc = env->ThrowNew(exception_class.get(), msg); 117430646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (rc != JNI_OK) { 117530646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI ThrowNew failed: " 117630646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes << PrettyTypeOf(GetException()); 117730646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes CHECK(IsExceptionPending()); 117830646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return; 117930646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1180a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes} 1181a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes 1182418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughesvoid Thread::ThrowOutOfMemoryError(Class* c, size_t byte_count) { 11832ced6a534157d5d963693346904389c19775d2daElliott Hughes std::string msg(StringPrintf("Failed to allocate a %zd-byte %s", byte_count, 11842ced6a534157d5d963693346904389c19775d2daElliott Hughes PrettyDescriptor(c->GetDescriptor()).c_str())); 11852ced6a534157d5d963693346904389c19775d2daElliott Hughes ThrowOutOfMemoryError(msg.c_str()); 11862ced6a534157d5d963693346904389c19775d2daElliott Hughes} 11872ced6a534157d5d963693346904389c19775d2daElliott Hughes 11882ced6a534157d5d963693346904389c19775d2daElliott Hughesvoid Thread::ThrowOutOfMemoryError(const char* msg) { 11892ced6a534157d5d963693346904389c19775d2daElliott Hughes LOG(ERROR) << StringPrintf("Throwing OutOfMemoryError \"%s\"%s", 11902ced6a534157d5d963693346904389c19775d2daElliott Hughes msg, (throwing_OutOfMemoryError_ ? " (recursive case)" : "")); 1191726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (!throwing_OutOfMemoryError_) { 1192726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_ = true; 1193accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes ThrowNewException("Ljava/lang/OutOfMemoryError;", NULL); 1194418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes } else { 1195726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes SetException(pre_allocated_OutOfMemoryError_); 1196418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes } 1197726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_ = false; 119879082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes} 119979082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes 1200accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1201accd83d1523545ac69bafd38e72a7d5cff8e2facElliott HughesThread* Thread::CurrentFromGdb() const { 1202accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes return Thread::Current(); 1203accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 1204accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1205accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughesvoid Thread::DumpFromGdb() const { 1206accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Dump(std::cerr); 1207accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 1208accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1209bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogersclass CatchBlockStackVisitor : public Thread::StackVisitor { 1210bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers public: 1211bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers CatchBlockStackVisitor(Class* to_find, Context* ljc) 121267375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers : found_(false), to_find_(to_find), long_jump_context_(ljc), native_method_count_(0) { 121367375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers#ifndef NDEBUG 121467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_pc_ = 0xEBADC0DE; 121567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_frame_.SetSP(reinterpret_cast<Method**>(0xEBADF00D)); 121667375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers#endif 121767375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers } 1218bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1219bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers virtual void VisitFrame(const Frame& fr, uintptr_t pc) { 1220bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (!found_) { 1221bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Method* method = fr.GetMethod(); 122267375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers if (method == NULL) { 122367375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // This is the upcall, we remember the frame and last_pc so that we may 122467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // long jump to them 1225a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes handler_pc_ = DemanglePc(pc); 122667375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_frame_ = fr; 122767375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers return; 122867375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers } 122967375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uint32_t dex_pc = DexFile::kDexNoIndex; 12303320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom if (method->IsCalleeSaveMethod()) { 12319086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // ignore callee save method 12329086572fa809d1a19d886b467e4da3ce42016982Ian Rogers } else if (method->IsNative()) { 12339086572fa809d1a19d886b467e4da3ce42016982Ian Rogers native_method_count_++; 12349086572fa809d1a19d886b467e4da3ce42016982Ian Rogers } else { 12359086572fa809d1a19d886b467e4da3ce42016982Ian Rogers dex_pc = method->ToDexPC(pc); 1236bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1237bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (dex_pc != DexFile::kDexNoIndex) { 1238bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers uint32_t found_dex_pc = method->FindCatchBlock(to_find_, dex_pc); 1239bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (found_dex_pc != DexFile::kDexNoIndex) { 1240bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers found_ = true; 124167375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_pc_ = method->ToNativePC(found_dex_pc); 124267375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_frame_ = fr; 1243bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1244bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1245bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (!found_) { 1246bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Caller may be handler, fill in callee saves in context 1247bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers long_jump_context_->FillCalleeSaves(fr); 1248bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 12491a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 12501a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 12511a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 1252bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Did we find a catch block yet? 1253bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers bool found_; 1254bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // The type of the exception catch block to find 1255bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Class* to_find_; 1256bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Frame with found handler or last frame if no handler found 1257bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Frame handler_frame_; 125867375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // PC to branch to for the handler 125967375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uintptr_t handler_pc_; 1260bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Context that will be the target of the long jump 1261bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Context* long_jump_context_; 126267375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // Number of native methods passed in crawl (equates to number of SIRTs to pop) 126367375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uint32_t native_method_count_; 1264bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers}; 1265bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1266ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogersvoid Thread::DeliverException() { 1267ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers Throwable *exception = GetException(); // Set exception on thread 1268ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers CHECK(exception != NULL); 1269bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1270bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Context* long_jump_context = GetLongJumpContext(); 1271bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers CatchBlockStackVisitor catch_finder(exception->GetClass(), long_jump_context); 127267375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers WalkStackUntilUpCall(&catch_finder, true); 1273bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 127467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers long_jump_context->SetSP(reinterpret_cast<intptr_t>(catch_finder.handler_frame_.GetSP())); 127567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers long_jump_context->SetPC(catch_finder.handler_pc_); 1276bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers long_jump_context->DoLongJump(); 1277bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers} 1278bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1279bdb0391258abc54bf77c676e36847d28a783bfe5Ian RogersContext* Thread::GetLongJumpContext() { 128085d1545e985ac689db4bad7849880e843707c862Elliott Hughes Context* result = long_jump_context_; 1281bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (result == NULL) { 1282bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers result = Context::Create(); 128385d1545e985ac689db4bad7849880e843707c862Elliott Hughes long_jump_context_ = result; 12841a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 1285bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers return result; 12861a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao} 12871a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 12889fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughesconst Method* Thread::GetCurrentMethod() const { 12899fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes Method* m = top_of_managed_stack_.GetMethod(); 12909fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // We use JNI internally for exception throwing, so it's possible to arrive 12919fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // here via a "FromCode" function, in which case there's a synthetic 12929fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // callee-save method at the top of the stack. These shouldn't be user-visible, 12939fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // so if we find one, skip it and return the compiled method underneath. 12949fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes if (m->IsCalleeSaveMethod()) { 12959fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes Frame f = top_of_managed_stack_; 12969fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes f.Next(); 12979fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes m = f.GetMethod(); 12989fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes } 12999fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes return m; 13009fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes} 13019fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes 13025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesbool Thread::HoldsLock(Object* object) { 13035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (object == NULL) { 13045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 13055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 13065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return object->GetLockOwner() == thin_lock_id_; 13075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 13085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1309038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesbool Thread::IsDaemon() { 1310038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes return gThread_daemon->GetBoolean(peer_); 1311038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes} 1312038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 1313d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogersclass ReferenceMapVisitor : public Thread::StackVisitor { 1314d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers public: 1315d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers ReferenceMapVisitor(Context* context, Heap::RootVisitor* root_visitor, void* arg) : 1316d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers context_(context), root_visitor_(root_visitor), arg_(arg) { 1317d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1318d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1319d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers void VisitFrame(const Frame& frame, uintptr_t pc) { 1320d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Method* m = frame.GetMethod(); 1321d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Process register map (which native and callee save methods don't have) 13223320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom if (!m->IsNative() && !m->IsCalleeSaveMethod()) { 1323d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers UniquePtr<art::DexVerifier::RegisterMap> map(art::DexVerifier::GetExpandedRegisterMap(m)); 1324f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers const uint8_t* reg_bitmap = art::DexVerifier::RegisterMapGetLine(map.get(), m->ToDexPC(pc)); 13259f865379c9ddf03f7363f362a8d63cb5d5b4e722Elliott Hughes if (false) { 13269f865379c9ddf03f7363f362a8d63cb5d5b4e722Elliott Hughes LOG(INFO) << "Visiting stack roots in " << PrettyMethod(m, false) 13279f865379c9ddf03f7363f362a8d63cb5d5b4e722Elliott Hughes << "@ PC: " << m->ToDexPC(pc); 13289f865379c9ddf03f7363f362a8d63cb5d5b4e722Elliott Hughes } 1329d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers CHECK(reg_bitmap != NULL); 13303320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom const uint16_t* vmap = m->GetVmapTable(); 1331d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // For all dex registers 1332d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers for (int reg = 0; reg < m->NumRegisters(); ++reg) { 1333d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Does this register hold a reference? 1334d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers if (TestBitmap(reg, reg_bitmap)) { 1335d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Is the reference in the context or on the stack? 1336d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers bool in_context = false; 1337f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers uint32_t vmap_offset = 0xEBAD0FF5; 1338d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // TODO: take advantage of the registers being ordered 13393320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom for (int i = 0; i < m->GetVmapTableLength(); i++) { 13403320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom if (vmap[i] == reg) { 1341d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers in_context = true; 1342d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers vmap_offset = i; 1343d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers break; 1344d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1345d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1346d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Object* ref; 1347d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers if (in_context) { 1348d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Compute the register we need to load from the context 1349d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers uint32_t spill_mask = m->GetCoreSpillMask(); 1350f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers uint32_t matches = 0; 1351f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers uint32_t spill_shifts = 0; 1352f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers while (matches != (vmap_offset + 1)) { 1353f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers CHECK_NE(spill_mask, 0u); 1354f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers matches += spill_mask & 1; // Add 1 if the low bit is set 1355f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_mask >>= 1; 1356f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_shifts++; 1357d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1358f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_shifts--; // wind back one as we want the last match 1359f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers ref = reinterpret_cast<Object*>(context_->GetGPR(spill_shifts)); 1360d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } else { 1361d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers ref = reinterpret_cast<Object*>(frame.GetVReg(m ,reg)); 1362d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 13634f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao if (ref != NULL) { 13644f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao root_visitor_(ref, arg_); 13654f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao } 1366d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1367d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1368d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1369d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers context_->FillCalleeSaves(frame); 1370d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1371d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1372d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers private: 1373d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers bool TestBitmap(int reg, const uint8_t* reg_vector) { 1374d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers return ((reg_vector[reg / 8] >> (reg % 8)) & 0x01) != 0; 1375d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1376d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1377d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Context used to build up picture of callee saves 1378d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Context* context_; 1379d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Call-back when we visit a root 1380d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Heap::RootVisitor* root_visitor_; 1381d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Argument to call-back 1382d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers void* arg_; 1383d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers}; 1384d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1385d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogersvoid Thread::VisitRoots(Heap::RootVisitor* visitor, void* arg) { 1386d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (exception_ != NULL) { 1387d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes visitor(exception_, arg); 1388d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 1389d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (peer_ != NULL) { 1390d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes visitor(peer_, arg); 1391d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 1392726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (pre_allocated_OutOfMemoryError_ != NULL) { 1393726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes visitor(pre_allocated_OutOfMemoryError_, arg); 1394726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 1395410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes jni_env_->locals.VisitRoots(visitor, arg); 1396410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes jni_env_->monitors.VisitRoots(visitor, arg); 13978dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 13988dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao SirtVisitRoots(visitor, arg); 13998dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 1400d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Cheat and steal the long jump context. Assume that we are not doing a GC during exception 1401d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // delivery. 1402d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Context* context = GetLongJumpContext(); 1403d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Visit roots on this thread's stack 1404d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers ReferenceMapVisitor mapper(context, visitor, arg); 1405d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers WalkStack(&mapper); 1406410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes} 1407410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes 1408b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogersstatic const char* kStateNames[] = { 140993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "Terminated", 1410b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers "Runnable", 141193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "TimedWaiting", 1412b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers "Blocked", 1413b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers "Waiting", 141493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "Initializing", 141593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "Starting", 1416b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers "Native", 141793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "VmWait", 141893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "Suspended", 1419b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers}; 1420b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogersstd::ostream& operator<<(std::ostream& os, const Thread::State& state) { 14218e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes int32_t int_state = static_cast<int32_t>(state); 142293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes if (state >= Thread::kTerminated && state <= Thread::kSuspended) { 142393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes os << kStateNames[int_state]; 1424b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers } else { 142593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes os << "State[" << int_state << "]"; 1426b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers } 1427b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers return os; 1428b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers} 1429b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers 1430330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughesstd::ostream& operator<<(std::ostream& os, const Thread& thread) { 1431330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes os << "Thread[" << &thread 1432e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes << ",tid=" << thread.GetTid() 1433dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes << ",id=" << thread.GetThinLockId() 14348daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes << ",state=" << thread.GetState() 14358daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes << ",peer=" << thread.GetPeer() 14368daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes << "]"; 1437330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes return os; 1438330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes} 1439330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes 14408daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} // namespace art 1441