thread.cc revision d07986fad0d08cdf05505cf9230714a2cf0dd9ae
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 29872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes#include "debugger.h" 30a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes#include "class_linker.h" 31df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom#include "class_loader.h" 32bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers#include "context.h" 33d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers#include "dex_verifier.h" 34408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers#include "heap.h" 35c5f7c91ab89055cffb573fff7e06dbdd860bccedElliott Hughes#include "jni_internal.h" 368e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes#include "monitor.h" 37a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes#include "object.h" 386d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h" 399a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson#include "reflection.h" 40578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "runtime.h" 415433072f589b61413e042eddf76e8190a048f71dbuzbee#include "runtime_support.h" 42726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes#include "ScopedLocalRef.h" 43aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#include "scoped_jni_thread_state.h" 4468e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack.h" 4568e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack_indirect_reference_table.h" 468daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "thread_list.h" 47a09576416788b916095739e43a16917e7948f3a4Elliott Hughes#include "utils.h" 48b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 49b557353b22c728eecbd1c68593b482622c7782a8Carl Shapironamespace art { 50b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 51b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiropthread_key_t Thread::pthread_key_self_; 52b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 538e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesstatic Class* gThreadLock = NULL; 5429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Class* gThrowable = NULL; 55038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_daemon = NULL; 56038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_group = NULL; 57038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_lock = NULL; 58038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_name = NULL; 59038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_priority = NULL; 6029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Field* gThread_uncaughtHandler = NULL; 61038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_vmData = NULL; 62038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThreadGroup_name = NULL; 638e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesstatic Field* gThreadLock_thread = NULL; 64038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Method* gThread_run = NULL; 6529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Method* gThreadGroup_removeThread = NULL; 6629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Method* gUncaughtExceptionHandler_uncaughtException = NULL; 67038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 685d76c435082332ef79a22962386fa92a0870e378Ian Rogersvoid Thread::InitCardTable() { 695d76c435082332ef79a22962386fa92a0870e378Ian Rogers card_table_ = Heap::GetCardTable()->GetBiasedBase(); 705d76c435082332ef79a22962386fa92a0870e378Ian Rogers} 715d76c435082332ef79a22962386fa92a0870e378Ian Rogers 723ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbeevoid Thread::InitFunctionPointers() { 735433072f589b61413e042eddf76e8190a048f71dbuzbee#if defined(__arm__) 745433072f589b61413e042eddf76e8190a048f71dbuzbee pShlLong = art_shl_long; 755433072f589b61413e042eddf76e8190a048f71dbuzbee pShrLong = art_shr_long; 765433072f589b61413e042eddf76e8190a048f71dbuzbee pUshrLong = art_ushr_long; 777b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pIdiv = __aeabi_idiv; 787b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pIdivmod = __aeabi_idivmod; 797b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pI2f = __aeabi_i2f; 807b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pF2iz = __aeabi_f2iz; 817b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pD2f = __aeabi_d2f; 827b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pF2d = __aeabi_f2d; 837b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pD2iz = __aeabi_d2iz; 847b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pL2f = __aeabi_l2f; 857b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pL2d = __aeabi_l2d; 867b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFadd = __aeabi_fadd; 877b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFsub = __aeabi_fsub; 887b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFdiv = __aeabi_fdiv; 897b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFmul = __aeabi_fmul; 907b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFmodf = fmodf; 917b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pDadd = __aeabi_dadd; 927b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pDsub = __aeabi_dsub; 937b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pDdiv = __aeabi_ddiv; 947b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pDmul = __aeabi_dmul; 957b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFmod = fmod; 967b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pLdivmod = __aeabi_ldivmod; 97439c4fa0db980fb19e4a585723a64a3461e4c278buzbee pLmul = __aeabi_lmul; 98b408de744566a5c5a80be1ba7f5c88407e816945Elliott Hughes pAllocArrayFromCode = art_alloc_array_from_code; 996fd03fb67e4628689f0abf34edeacc0e35b6295eBrian Carlstrom pAllocObjectFromCode = art_alloc_object_from_code; 10028ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers pAllocObjectFromCodeWithAccessCheck = art_alloc_object_from_code_with_access_check; 101e51a511ccee3f3c0120807321bcc160fcaa664beIan Rogers pCanPutArrayElementFromCode = art_can_put_array_element_from_code; 102b408de744566a5c5a80be1ba7f5c88407e816945Elliott Hughes pCheckAndAllocArrayFromCode = art_check_and_alloc_array_from_code; 103ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pCheckCastFromCode = art_check_cast_from_code; 104ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pFindInstanceFieldFromCode = art_find_instance_field_from_code; 105ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pGet32Static = art_get32_static_from_code; 106ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pGet64Static = art_get64_static_from_code; 107ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pGetObjStatic = art_get_obj_static_from_code; 108ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pHandleFillArrayDataFromCode = art_handle_fill_data_from_code; 109cbba6ac9bf9a6c630a7aafae6d8767b5ddbb6fd5Ian Rogers pInitializeStaticStorage = art_initialize_static_storage_from_code; 11028ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers pInitializeTypeFromCode = art_initialize_type_from_code; 111b093c6b27f8ea9bbe2d49c03ebe345203a121199Ian Rogers pInitializeTypeAndVerifyAccessFromCode = art_initialize_type_and_verify_access_from_code; 1124a3164faefd255b1c1e911e7ad7c3d57749caaf6buzbee pInvokeInterfaceTrampoline = art_invoke_interface_trampoline; 1136fd03fb67e4628689f0abf34edeacc0e35b6295eBrian Carlstrom pLockObjectFromCode = art_lock_object_from_code; 1146fd03fb67e4628689f0abf34edeacc0e35b6295eBrian Carlstrom pObjectInit = art_object_init_from_code; 1156fd03fb67e4628689f0abf34edeacc0e35b6295eBrian Carlstrom pResolveStringFromCode = art_resolve_string_from_code; 116ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pSet32Static = art_set32_static_from_code; 117ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pSet64Static = art_set64_static_from_code; 118ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pSetObjStatic = art_set_obj_static_from_code; 1196fd03fb67e4628689f0abf34edeacc0e35b6295eBrian Carlstrom pTestSuspendFromCode = art_test_suspend; 120ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pThrowArrayBoundsFromCode = art_throw_array_bounds_from_code; 121ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pThrowDivZeroFromCode = art_throw_div_zero_from_code; 122c0c8dc8e677957d167066a2d06973126f108a6aaIan Rogers pThrowNegArraySizeFromCode = art_throw_neg_array_size_from_code; 123c0c8dc8e677957d167066a2d06973126f108a6aaIan Rogers pThrowNoSuchMethodFromCode = art_throw_no_such_method_from_code; 124ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pThrowNullPointerFromCode = art_throw_null_pointer_exception_from_code; 125932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers pThrowStackOverflowFromCode = art_throw_stack_overflow_from_code; 126c0c8dc8e677957d167066a2d06973126f108a6aaIan Rogers pThrowVerificationErrorFromCode = art_throw_verification_error_from_code; 127ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pUnlockObjectFromCode = art_unlock_object_from_code; 12867375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers#endif 129c396efc1baec875160afe34a3e65c46cce2bd72fbuzbee pF2l = F2L; 130c396efc1baec875160afe34a3e65c46cce2bd72fbuzbee pD2l = D2L; 1313ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee pMemcpy = memcpy; 1324a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pCheckSuspendFromCode = CheckSuspendFromCode; 1334a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pDebugMe = DebugMe; 1344a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pDecodeJObjectInThread = DecodeJObjectInThread; 1354a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pDeliverException = art_deliver_exception_from_code; 1364a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pFindNativeMethod = FindNativeMethod; 137caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers pInstanceofNonTrivialFromCode = IsAssignableFromCode; 1384a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pResolveMethodFromCode = ResolveMethodFromCode; 1394a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pThrowAbstractMethodErrorFromCode = ThrowAbstractMethodErrorFromCode; 1404a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pUnresolvedDirectMethodTrampolineFromCode = UnresolvedDirectMethodTrampolineFromCode; 1413ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee} 1423ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee 143caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstromvoid Thread::InitTid() { 144caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom tid_ = ::art::GetTid(); 145caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom} 146caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom 147caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstromvoid Thread::InitAfterFork() { 148caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom InitTid(); 1493147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes#if defined(__BIONIC__) 1503147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes // Work around a bionic bug. 1513147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes struct bionic_pthread_internal_t { 1523147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes void* next; 1533147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes void** pref; 1543147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes pthread_attr_t attr; 1553147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes pid_t kernel_id; 1563147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes // et cetera. we just need 'kernel_id' so we can stop here. 1573147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes }; 1583147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes reinterpret_cast<bionic_pthread_internal_t*>(pthread_self())->kernel_id = tid_; 1593147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes#endif 160caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom} 161caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom 16278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromvoid* Thread::CreateCallback(void* arg) { 16393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* self = reinterpret_cast<Thread*>(arg); 16493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Runtime* runtime = Runtime::Current(); 16593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 16693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes self->Attach(runtime); 16793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 16893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Wait until it's safe to start running code. (There may have been a suspend-all 16993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // in progress while we were starting up.) 17093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes runtime->GetThreadList()->WaitForGo(); 17193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 17247179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes { 17347179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_EQ(self->GetState(), Thread::kRunnable); 17400fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom SirtRef<String> thread_name(self->GetName()); 17500fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom SetThreadName(thread_name->ToModifiedUtf8().c_str()); 17647179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes } 17747179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes 178872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes Dbg::PostThreadStart(self); 17993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 18093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Invoke the 'run' method of our java.lang.Thread. 18193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes CHECK(self->peer_ != NULL); 18293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Object* receiver = self->peer_; 183038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes Method* m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(gThread_run); 18493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes m->Invoke(self, receiver, NULL, NULL); 18593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 18693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Detach. 18793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes runtime->GetThreadList()->Unregister(); 18893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 189b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro return NULL; 190b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 191b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 19293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughesvoid SetVmData(Object* managed_thread, Thread* native_thread) { 193038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes gThread_vmData->SetInt(managed_thread, reinterpret_cast<uintptr_t>(native_thread)); 19493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 19593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 196761928d24e4e7ed7776b52243eaf9095ad35f448Elliott HughesThread* Thread::FromManagedThread(Object* thread_peer) { 197761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes return reinterpret_cast<Thread*>(static_cast<uintptr_t>(gThread_vmData->GetInt(thread_peer))); 198761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes} 199761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes 20001158d7a57c8321370667a6045220237d16e0da8Elliott HughesThread* Thread::FromManagedThread(JNIEnv* env, jobject java_thread) { 201761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes return FromManagedThread(Decode<Object*>(env, java_thread)); 20201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes} 20301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 2047502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughessize_t FixStackSize(size_t stack_size) { 2057502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // A stack size of zero means "use the default". 206d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (stack_size == 0) { 207d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes stack_size = Runtime::Current()->GetDefaultStackSize(); 208d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 20961e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 2107502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // It's not possible to request a stack smaller than the system-defined PTHREAD_STACK_MIN. 2117502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes if (stack_size < PTHREAD_STACK_MIN) { 2127502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = PTHREAD_STACK_MIN; 2137502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes } 2147502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2157502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // It's likely that callers are trying to ensure they have at least a certain amount of 2167502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // stack space, so we should add our reserved space on top of what they requested, rather 2177502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // than implicitly take it away from them. 2187502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size += Thread::kStackOverflowReservedBytes; 2197502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2207502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // Some systems require the stack size to be a multiple of the system page size, so round up. 2217502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = RoundUp(stack_size, kPageSize); 2227502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2237502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes return stack_size; 2247502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes} 2257502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2267502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughesvoid Thread::Create(Object* peer, size_t stack_size) { 2277502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes CHECK(peer != NULL); 2287502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2297502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = FixStackSize(stack_size); 2307502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 23193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* native_thread = new Thread; 23293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes native_thread->peer_ = peer; 23393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 23493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Thread.start is synchronized, so we know that vmData is 0, 23593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // and know that we're not racing to assign it. 23693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes SetVmData(peer, native_thread); 23761e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 23847179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes { 23947179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait); 24047179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes pthread_t new_pthread; 24147179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes pthread_attr_t attr; 24247179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread"); 24347179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), "PTHREAD_CREATE_DETACHED"); 24447179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size); 24547179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_create, (&new_pthread, &attr, Thread::CreateCallback, native_thread), "new thread"); 24647179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread"); 24747179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes } 24861e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 24993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Let the child know when it's safe to start running. 25093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Runtime::Current()->GetThreadList()->SignalGo(native_thread); 25193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 25261e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 25393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughesvoid Thread::Attach(const Runtime* runtime) { 254cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes CHECK(Thread::Current() == NULL); 255cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes 25693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitCpu(); 25793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitFunctionPointers(); 2585d76c435082332ef79a22962386fa92a0870e378Ian Rogers InitCardTable(); 25961e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 26093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes thin_lock_id_ = Runtime::Current()->GetThreadList()->AllocThreadId(); 261be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 262caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom InitTid(); 26393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitStackHwm(); 264dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 2653147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach"); 266a5780dad67556297c8ca5f2608c53b193e6c4514Elliott Hughes 26793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes jni_env_ = new JNIEnvExt(this, runtime->GetJavaVM()); 2685fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 2697a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes runtime->GetThreadList()->Register(); 27093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 27193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 27293e74e8d879270071c3aa163f8495ada8d21f42fElliott HughesThread* Thread::Attach(const Runtime* runtime, const char* name, bool as_daemon) { 273cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes Thread* self = new Thread; 274cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes self->Attach(runtime); 2755fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 276cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes self->SetState(Thread::kNative); 277726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes 278cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes // If we're the main thread, ClassLinker won't be created until after we're attached, 279cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes // so that thread needs a two-stage attach. Regular threads don't need this hack. 280cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes if (self->thin_lock_id_ != ThreadList::kMainId) { 281cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes self->CreatePeer(name, as_daemon); 28236e0a955e48e9357bff1eab783e493107ebfff62Ian Rogers } 283cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes 284cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes self->GetJniEnv()->locals.AssertEmpty(); 2855fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes return self; 2865fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes} 2875fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 288d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughesjobject GetWellKnownThreadGroup(JNIEnv* env, const char* field_name) { 289726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jclass> thread_group_class(env, env->FindClass("java/lang/ThreadGroup")); 290726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes jfieldID fid = env->GetStaticFieldID(thread_group_class.get(), field_name, "Ljava/lang/ThreadGroup;"); 291726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes return env->GetStaticObjectField(thread_group_class.get(), fid); 292d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes} 293d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 2945fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughesvoid Thread::CreatePeer(const char* name, bool as_daemon) { 2959a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson CHECK(Runtime::Current()->IsStarted()); 2965fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes JNIEnv* env = jni_env_; 2975fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 298d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes const char* field_name = (GetThinLockId() == ThreadList::kMainId) ? "mMain" : "mSystem"; 299726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jobject> thread_group(env, GetWellKnownThreadGroup(env, field_name)); 300726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF(name)); 3018daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes jint thread_priority = GetNativePriority(); 3025fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes jboolean thread_is_daemon = as_daemon; 3035fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 304726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Thread")); 3055d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers ScopedLocalRef<jobject> peer(env, env->AllocObject(c.get())); 306726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes peer_ = DecodeJObject(peer.get()); 3075d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers if (peer_ == NULL) { 3085d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers CHECK(IsExceptionPending()); 3095d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers return; 3105d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers } 3115d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers jmethodID mid = env->GetMethodID(c.get(), "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V"); 3125d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers env->CallNonvirtualVoidMethod(peer.get(), c.get(), mid, thread_group.get(), thread_name.get(), thread_priority, thread_is_daemon); 3139a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson CHECK(!IsExceptionPending()) << " " << PrettyTypeOf(GetException()); 3147a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes SetVmData(peer_, Thread::Current()); 315d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 31600fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom SirtRef<String> peer_thread_name(GetName()); 31700fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom if (peer_thread_name.get() == NULL) { 31800fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // The Thread constructor should have set the Thread.name to a 31900fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // non-null value. However, because we can run without code 32000fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // available (in the compiler, in tests), we manually assign the 32100fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // fields the constructor should have set. 32200fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom gThread_daemon->SetBoolean(peer_, thread_is_daemon); 32300fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom gThread_group->SetObject(peer_, Decode<Object*>(env, thread_group.get())); 32400fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom gThread_name->SetObject(peer_, Decode<Object*>(env, thread_name.get())); 32500fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom gThread_priority->SetInt(peer_, thread_priority); 32600fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom peer_thread_name.reset(GetName()); 32700fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom } 32800fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // thread_name may have been null, so don't trust this to be non-null 32900fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom if (peer_thread_name.get() != NULL) { 33000fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom SetThreadName(GetName()->ToModifiedUtf8().c_str()); 33100fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom } 332726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes 333726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes // Pre-allocate an OutOfMemoryError for the double-OOME case. 334726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ThrowNewException("Ljava/lang/OutOfMemoryError;", 335726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes "OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available"); 336726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jthrowable> exception(env, env->ExceptionOccurred()); 337726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes env->ExceptionClear(); 338726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes pre_allocated_OutOfMemoryError_ = Decode<Throwable*>(env, exception.get()); 33961e019d291583029c01b61b93bea750f2b663c37Carl Shapiro} 34061e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 341be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::InitStackHwm() { 342be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes pthread_attr_t attributes; 3433147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes CHECK_PTHREAD_CALL(pthread_getattr_np, (pthread_self(), &attributes), __FUNCTION__); 344be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 345932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers void* temp_stack_base; 346932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, &temp_stack_base, &stack_size_), 347932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers __FUNCTION__); 348932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers stack_base_ = reinterpret_cast<byte*>(temp_stack_base); 349be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 350932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers if (stack_size_ <= kStackOverflowReservedBytes) { 351932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers LOG(FATAL) << "attempt to attach a thread with a too-small stack (" << stack_size_ << " bytes)"; 352be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 353449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes 354932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // Set stack_end_ to the bottom of the stack saving space of stack overflows 355932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers ResetDefaultStackEnd(); 356449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes 357449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes // Sanity check. 358449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes int stack_variable; 359449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes CHECK_GT(&stack_variable, (void*) stack_end_); 360be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 3618d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); 362be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes} 363be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 364e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughesvoid Thread::Dump(std::ostream& os, bool dump_pending_exception) const { 365d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes DumpState(os); 366d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes DumpStack(os); 367e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes if (dump_pending_exception && IsExceptionPending()) { 368e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes os << "Pending " << PrettyTypeOf(GetException()) << " on thread:\n"; 369e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes os << GetException()->Dump(); 370e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes } 371d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes} 372d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 373d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesstd::string GetSchedulerGroup(pid_t tid) { 374d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // /proc/<pid>/group looks like this: 375d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // 2:devices:/ 376d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // 1:cpuacct,cpu:/ 377d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // We want the third field from the line whose second field contains the "cpu" token. 378d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string cgroup_file; 379d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (!ReadFileToString("/proc/self/cgroup", &cgroup_file)) { 380d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes return ""; 381d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 382d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::vector<std::string> cgroup_lines; 383d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes Split(cgroup_file, '\n', cgroup_lines); 384d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes for (size_t i = 0; i < cgroup_lines.size(); ++i) { 385d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::vector<std::string> cgroup_fields; 386d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes Split(cgroup_lines[i], ':', cgroup_fields); 387d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::vector<std::string> cgroups; 388d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes Split(cgroup_fields[1], ',', cgroups); 389d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes for (size_t i = 0; i < cgroups.size(); ++i) { 390d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (cgroups[i] == "cpu") { 391d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes return cgroup_fields[2].substr(1); // Skip the leading slash. 392d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 393d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 394d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 395d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes return ""; 396d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes} 397d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 398fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott HughesString* Thread::GetName() const { 399fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes return (peer_ != NULL) ? reinterpret_cast<String*>(gThread_name->GetObject(peer_)) : NULL; 400fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes} 401fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 402d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesvoid Thread::DumpState(std::ostream& os) const { 403d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::string thread_name("<native thread without managed peer>"); 404d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::string group_name; 405d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes int priority; 406d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes bool is_daemon = false; 407d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 408d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (peer_ != NULL) { 409038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes String* thread_name_string = reinterpret_cast<String*>(gThread_name->GetObject(peer_)); 410d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes thread_name = (thread_name_string != NULL) ? thread_name_string->ToModifiedUtf8() : "<null>"; 411038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes priority = gThread_priority->GetInt(peer_); 412038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes is_daemon = gThread_daemon->GetBoolean(peer_); 413d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 414a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes Object* thread_group = GetThreadGroup(); 415d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (thread_group != NULL) { 416038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes String* group_name_string = reinterpret_cast<String*>(gThreadGroup_name->GetObject(thread_group)); 417d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes group_name = (group_name_string != NULL) ? group_name_string->ToModifiedUtf8() : "<null>"; 418d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 419d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } else { 420d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes // This name may be truncated, but it's the best we can do in the absence of a managed peer. 421dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes std::string stats; 422dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes if (ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { 423dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes size_t start = stats.find('(') + 1; 424dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes size_t end = stats.find(')') - start; 425dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes thread_name = stats.substr(start, end); 426dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes } 427d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes priority = GetNativePriority(); 428dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes } 429d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 430d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int policy; 431d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes sched_param sp; 4323147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes CHECK_PTHREAD_CALL(pthread_getschedparam, (pthread_self(), &policy, &sp), __FUNCTION__); 433d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 434d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string scheduler_group(GetSchedulerGroup(GetTid())); 435d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (scheduler_group.empty()) { 436d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_group = "default"; 437d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 438d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 439d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << '"' << thread_name << '"'; 440d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (is_daemon) { 441d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " daemon"; 442d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 443d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " prio=" << priority 444dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes << " tid=" << GetThinLockId() 44593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes << " " << GetState() << "\n"; 446d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 447d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | group=\"" << group_name << "\"" 4488d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes << " sCount=" << suspend_count_ 449234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes << " dsCount=" << debug_suspend_count_ 450dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes << " obj=" << reinterpret_cast<void*>(peer_) 451d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " self=" << reinterpret_cast<const void*>(this) << "\n"; 452d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | sysTid=" << GetTid() 453d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " nice=" << getpriority(PRIO_PROCESS, GetTid()) 454d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " sched=" << policy << "/" << sp.sched_priority 455d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " cgrp=" << scheduler_group 4563147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes << " handle=" << pthread_self() << "\n"; 457d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 458d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // Grab the scheduler stats for this thread. 459d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string scheduler_stats; 460d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", GetTid()).c_str(), &scheduler_stats)) { 461d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'. 462d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } else { 463d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_stats = "0 0 0"; 464d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 465d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 466d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int utime = 0; 467d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int stime = 0; 468d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int task_cpu = 0; 469bfe487be25652c5456236661b9d9c3579d2296c1Elliott Hughes GetTaskStats(GetTid(), utime, stime, task_cpu); 470d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 471d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | schedstat=( " << scheduler_stats << " )" 472d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " utm=" << utime 473d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " stm=" << stime 474d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " core=" << task_cpu 475d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " HZ=" << sysconf(_SC_CLK_TCK) << "\n"; 476a09576416788b916095739e43a16917e7948f3a4Elliott Hughes} 477a09576416788b916095739e43a16917e7948f3a4Elliott Hughes 478b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogersvoid Thread::PushNativeToManagedRecord(NativeToManagedRecord* record) { 479b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers Method **sp = top_of_managed_stack_.GetSP(); 480b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers#ifndef NDEBUG 481b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers if (sp != NULL) { 482b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers Method* m = *sp; 483b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers Heap::VerifyObject(m); 484b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers DCHECK((m == NULL) || m->IsMethod()); 485b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers } 486b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers#endif 487b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers record->last_top_of_managed_stack_ = reinterpret_cast<void*>(sp); 488b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers record->last_top_of_managed_stack_pc_ = top_of_managed_stack_pc_; 489b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers record->link_ = native_to_managed_record_; 490b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers native_to_managed_record_ = record; 491b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers top_of_managed_stack_.SetSP(NULL); 492b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers} 493b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers 494b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogersvoid Thread::PopNativeToManagedRecord(const NativeToManagedRecord& record) { 495b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers native_to_managed_record_ = record.link_; 496b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers top_of_managed_stack_.SetSP(reinterpret_cast<Method**>(record.last_top_of_managed_stack_)); 497b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers top_of_managed_stack_pc_ = record.last_top_of_managed_stack_pc_; 498b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers} 499b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers 500d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughesstruct StackDumpVisitor : public Thread::StackVisitor { 5018e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes StackDumpVisitor(std::ostream& os, const Thread* thread) 50228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers : last_method(NULL), last_line_number(0), repetition_count(0), os(os), thread(thread), 50328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers frame_count(0) { 504d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 505d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 506bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers virtual ~StackDumpVisitor() { 507d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 508d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 509bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers void VisitFrame(const Frame& frame, uintptr_t pc) { 5109086572fa809d1a19d886b467e4da3ce42016982Ian Rogers if (!frame.HasMethod()) { 5119086572fa809d1a19d886b467e4da3ce42016982Ian Rogers return; 5129086572fa809d1a19d886b467e4da3ce42016982Ian Rogers } 51328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers const int kMaxRepetition = 3; 514d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Method* m = frame.GetMethod(); 515d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Class* c = m->GetDeclaringClass(); 5168e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 517b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers const DexCache* dex_cache = c->GetDexCache(); 518b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers int line_number = -1; 519b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers if (dex_cache != NULL) { // be tolerant of bad input 520b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers const DexFile& dex_file = class_linker->FindDexFile(dex_cache); 521b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers line_number = dex_file.GetLineNumFromPC(m, m->ToDexPC(pc)); 522b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers } 52328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (line_number == last_line_number && last_method == m) { 52428ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers repetition_count++; 525d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } else { 52628ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (repetition_count >= kMaxRepetition) { 52728ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << " ... repeated " << (repetition_count - kMaxRepetition) << " times\n"; 52828ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 52928ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers repetition_count = 0; 53028ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers last_line_number = line_number; 53128ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers last_method = m; 53228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 53328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (repetition_count < kMaxRepetition) { 53428ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << " at " << PrettyMethod(m, false); 53528ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (m->IsNative()) { 53628ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << "(Native method)"; 53728ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } else { 5386d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers mh.ChangeMethod(m); 5396d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers const char* source_file(mh.GetDeclaringClassSourceFile()); 5406d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers os << "(" << (source_file != NULL ? source_file : "unavailable") 54128ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers << ":" << line_number << ")"; 54228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 54328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << "\n"; 544d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 5458e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 5468e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (frame_count++ == 0) { 5478e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Monitor::DescribeWait(os, thread); 5488e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 549d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 5506d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers MethodHelper mh; 55128ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers Method* last_method; 55228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers int last_line_number; 55328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers int repetition_count; 554d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::ostream& os; 5558e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes const Thread* thread; 5568e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes int frame_count; 557d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes}; 558d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 559d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesvoid Thread::DumpStack(std::ostream& os) const { 5608e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes StackDumpVisitor dumper(os, this); 561d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes WalkStack(&dumper); 562e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes} 563e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes 5648d768a954b101a9532f980253ac46be2c53aba11Elliott HughesThread::State Thread::SetState(Thread::State new_state) { 5658d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes Thread::State old_state = state_; 5668d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (old_state == new_state) { 5678d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes return old_state; 5688d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 5698d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 5708d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes volatile void* raw = reinterpret_cast<volatile void*>(&state_); 5718d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes volatile int32_t* addr = reinterpret_cast<volatile int32_t*>(raw); 5728d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 5738d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (new_state == Thread::kRunnable) { 5748d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes /* 5758d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Change our status to Thread::kRunnable. The transition requires 5768d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * that we check for pending suspension, because the VM considers 5778d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * us to be "asleep" in all other states, and another thread could 5788d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * be performing a GC now. 5798d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5808d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * The order of operations is very significant here. One way to 5818d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * do this wrong is: 5828d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5838d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * GCing thread Our thread (in kNative) 5848d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * ------------ ---------------------- 5858d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * check suspend count (== 0) 5868d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * SuspendAllThreads() 5878d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * grab suspend-count lock 5888d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * increment all suspend counts 5898d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * release suspend-count lock 5908d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * check thread state (== kNative) 5918d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * all are suspended, begin GC 5928d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * set state to kRunnable 5938d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * (continue executing) 5948d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5958d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We can correct this by grabbing the suspend-count lock and 5968d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * performing both of our operations (check suspend count, set 5978d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * state) while holding it, now we need to grab a mutex on every 5988d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * transition to kRunnable. 5998d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6008d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * What we do instead is change the order of operations so that 6018d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * the transition to kRunnable happens first. If we then detect 6028d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * that the suspend count is nonzero, we switch to kSuspended. 6038d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6048d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Appropriate compiler and memory barriers are required to ensure 6058d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * that the operations are observed in the expected order. 6068d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6078d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * This does create a small window of opportunity where a GC in 6088d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * progress could observe what appears to be a running thread (if 6098d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * it happens to look between when we set to kRunnable and when we 6108d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * switch to kSuspended). At worst this only affects assertions 6118d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * and thread logging. (We could work around it with some sort 6128d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * of intermediate "pre-running" state that is generally treated 6138d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * as equivalent to running, but that doesn't seem worthwhile.) 6148d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6158d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We can also solve this by combining the "status" and "suspend 6168d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * count" fields into a single 32-bit value. This trades the 6178d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * store/load barrier on transition to kRunnable for an atomic RMW 6188d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * op on all transitions and all suspend count updates (also, all 6198d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * accesses to status or the thread count require bit-fiddling). 6208d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * It also eliminates the brief transition through kRunnable when 6218d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * the thread is supposed to be suspended. This is possibly faster 6228d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * on SMP and slightly more correct, but less convenient. 6238d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes */ 6248d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes android_atomic_acquire_store(new_state, addr); 6258d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (ANNOTATE_UNPROTECTED_READ(suspend_count_) != 0) { 6268d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes Runtime::Current()->GetThreadList()->FullSuspendCheck(this); 6278d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 6288d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } else { 6298d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes /* 6308d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Not changing to Thread::kRunnable. No additional work required. 6318d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6328d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We use a releasing store to ensure that, if we were runnable, 6338d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * any updates we previously made to objects on the managed heap 6348d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * will be observed before the state change. 6358d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes */ 6368d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes android_atomic_release_store(new_state, addr); 6378d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 6388d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 6398d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes return old_state; 6408d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes} 6418d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 642761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughesbool Thread::IsSuspended() { 643761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes return ANNOTATE_UNPROTECTED_READ(suspend_count_) != 0 && GetState() != Thread::kRunnable; 644761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes} 645761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes 6468d768a954b101a9532f980253ac46be2c53aba11Elliott Hughesvoid Thread::WaitUntilSuspended() { 6478d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes // TODO: dalvik dropped the waiting thread's priority after a while. 6488d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes // TODO: dalvik timed out and aborted. 6498d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes useconds_t delay = 0; 6508d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes while (GetState() == Thread::kRunnable) { 6518d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes useconds_t new_delay = delay * 2; 6528d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_GE(new_delay, delay); 6538d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes delay = new_delay; 6548d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (delay == 0) { 6558d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes sched_yield(); 6568d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes delay = 10000; 6578d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } else { 6588d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes usleep(delay); 6598d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 6608d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 6618d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes} 6628d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 663be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::ThreadExitCallback(void* arg) { 664be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes Thread* self = reinterpret_cast<Thread*>(arg); 665be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self; 666b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 667b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 668be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::Startup() { 669b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro // Allocate a TLS slot. 6708d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_key_create, (&Thread::pthread_key_self_, Thread::ThreadExitCallback), "self key"); 671b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 672b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro // Double-check the TLS slot allocation. 673b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro if (pthread_getspecific(pthread_key_self_) != NULL) { 674be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes LOG(FATAL) << "newly-created pthread TLS slot is not NULL"; 675b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro } 676038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes} 677038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 6788e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 6798e4aac52962d54cb4be2078b9cd95685e067133aElliott HughesClass* FindPrimitiveClassOrDie(ClassLinker* class_linker, char descriptor) { 6808e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* c = class_linker->FindPrimitiveClass(descriptor); 6818e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(c != NULL) << descriptor; 6828e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return c; 6838e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 6848e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6858e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 6868e4aac52962d54cb4be2078b9cd95685e067133aElliott HughesClass* FindClassOrDie(ClassLinker* class_linker, const char* descriptor) { 6878e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* c = class_linker->FindSystemClass(descriptor); 6888e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(c != NULL) << descriptor; 6898e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return c; 6908e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 6918e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6928e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 6936b4ef025af12b158d117fc80fc79acf620f411a0Brian CarlstromField* FindFieldOrDie(Class* c, const char* name, const char* descriptor) { 6946b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom Field* f = c->FindDeclaredInstanceField(name, descriptor); 6956b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom CHECK(f != NULL) << PrettyClass(c) << " " << name << " " << descriptor; 6968e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return f; 6978e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 6988e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6998e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 7008e4aac52962d54cb4be2078b9cd95685e067133aElliott HughesMethod* FindMethodOrDie(Class* c, const char* name, const char* signature) { 7018e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Method* m = c->FindVirtualMethod(name, signature); 7028e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(m != NULL) << PrettyClass(c) << " " << name << " " << signature; 7038e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return m; 7048e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 7058e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 706038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesvoid Thread::FinishStartup() { 7079a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson CHECK(Runtime::Current()->IsStarted()); 708b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom Thread* self = Thread::Current(); 709b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom 710b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom // Need to be kRunnable for FindClass 711b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom ScopedThreadStateChange tsc(self, Thread::kRunnable); 712b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom 713038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes // Now the ClassLinker is ready, we can find the various Class*, Field*, and Method*s we need. 714038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 7158e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 7168e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* Thread_class = FindClassOrDie(class_linker, "Ljava/lang/Thread;"); 7178e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* ThreadGroup_class = FindClassOrDie(class_linker, "Ljava/lang/ThreadGroup;"); 7188e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* UncaughtExceptionHandler_class = FindClassOrDie(class_linker, "Ljava/lang/Thread$UncaughtExceptionHandler;"); 7198e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThreadLock = FindClassOrDie(class_linker, "Ljava/lang/ThreadLock;"); 7208e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThrowable = FindClassOrDie(class_linker, "Ljava/lang/Throwable;"); 7218e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 7226b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_daemon = FindFieldOrDie(Thread_class, "daemon", "Z"); 7236b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_group = FindFieldOrDie(Thread_class, "group", "Ljava/lang/ThreadGroup;"); 7246b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_lock = FindFieldOrDie(Thread_class, "lock", "Ljava/lang/ThreadLock;"); 7256b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_name = FindFieldOrDie(Thread_class, "name", "Ljava/lang/String;"); 7266b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_priority = FindFieldOrDie(Thread_class, "priority", "I"); 7276b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_uncaughtHandler = FindFieldOrDie(Thread_class, "uncaughtHandler", "Ljava/lang/Thread$UncaughtExceptionHandler;"); 7286b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_vmData = FindFieldOrDie(Thread_class, "vmData", "I"); 7296b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThreadGroup_name = FindFieldOrDie(ThreadGroup_class, "name", "Ljava/lang/String;"); 7306b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThreadLock_thread = FindFieldOrDie(gThreadLock, "thread", "Ljava/lang/Thread;"); 7318e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 7328e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_run = FindMethodOrDie(Thread_class, "run", "()V"); 7338e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThreadGroup_removeThread = FindMethodOrDie(ThreadGroup_class, "removeThread", "(Ljava/lang/Thread;)V"); 7348e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gUncaughtExceptionHandler_uncaughtException = FindMethodOrDie(UncaughtExceptionHandler_class, 7358e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes "uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V"); 73601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 73701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Finish attaching the main thread. 7389a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson Thread::Current()->CreatePeer("main", false); 7399a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson 7409a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson InitBoxingMethods(); 7419a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson class_linker->RunRootClinits(); 742b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 743b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 744c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughesvoid Thread::Shutdown() { 7458d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key"); 746c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes} 747c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes 7488e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesuint32_t Thread::LockOwnerFromThreadLock(Object* thread_lock) { 7498e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (thread_lock == NULL || thread_lock->GetClass() != gThreadLock) { 7508e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 7518e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 7528e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Object* managed_thread = gThreadLock_thread->GetObject(thread_lock); 7538e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (managed_thread == NULL) { 7548e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 7558e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 7568e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes uintptr_t vmData = static_cast<uintptr_t>(gThread_vmData->GetInt(managed_thread)); 7578e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Thread* thread = reinterpret_cast<Thread*>(vmData); 7588e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (thread == NULL) { 7598e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 7608e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 7618e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return thread->GetThinLockId(); 7628e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 7638e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 764dcc247493fd8fb243e335c3ec08e5e625896a47cElliott HughesThread::Thread() 76547179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes : thin_lock_id_(0), 76647179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes tid_(0), 76747179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes peer_(NULL), 7688e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes top_of_managed_stack_(), 7698e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes top_of_managed_stack_pc_(0), 77085d1545e985ac689db4bad7849880e843707c862Elliott Hughes wait_mutex_(new Mutex("Thread wait mutex")), 77185d1545e985ac689db4bad7849880e843707c862Elliott Hughes wait_cond_(new ConditionVariable("Thread wait condition variable")), 7728daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes wait_monitor_(NULL), 7738daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes interrupted_(false), 774dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes wait_next_(NULL), 7758e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes monitor_enter_object_(NULL), 776dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes card_table_(0), 7778daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes stack_end_(NULL), 778dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes native_to_managed_record_(NULL), 779dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes top_sirt_(NULL), 780dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes jni_env_(NULL), 7818e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes state_(Thread::kNative), 782dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes self_(NULL), 783dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes runtime_(NULL), 784dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes exception_(NULL), 785dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes suspend_count_(0), 786234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes debug_suspend_count_(0), 78785d1545e985ac689db4bad7849880e843707c862Elliott Hughes class_loader_override_(NULL), 788418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes long_jump_context_(NULL), 789726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_(false), 790475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes pre_allocated_OutOfMemoryError_(NULL), 791475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes debug_invoke_req_(new DebugInvokeReq) { 792f5a7a476e7ea63e094ff0f011dccc170607e6f6bElliott Hughes CHECK_EQ((sizeof(Thread) % 4), 0U) << sizeof(Thread); 793dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes} 794dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 79502b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughesvoid MonitorExitVisitor(const Object* object, void*) { 79602b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes Object* entered_monitor = const_cast<Object*>(object); 7975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes entered_monitor->MonitorExit(Thread::Current()); 79802b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes} 79902b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 800c1674ed06662420213441ff2b818f2f71f9098dcElliott HughesThread::~Thread() { 80102b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited. 80293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes if (jni_env_ != NULL) { 80393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes jni_env_->monitors.VisitRoots(MonitorExitVisitor, NULL); 80493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes } 80502b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 80629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (peer_ != NULL) { 80729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 80829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // this.vmData = 0; 80993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes SetVmData(peer_, NULL); 81002b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 811872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes Dbg::PostThreadDeath(this); 81202b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 81329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // Thread.join() is implemented as an Object.wait() on the Thread.lock 81429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // object. Signal anyone who is waiting. 8155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* self = Thread::Current(); 816038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes Object* lock = gThread_lock->GetObject(peer_); 817038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes // (This conditional is only needed for tests, where Thread.lock won't have been set.) 8185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (lock != NULL) { 8195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->MonitorEnter(self); 8205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->NotifyAll(); 8215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->MonitorExit(self); 8225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 8235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 82402b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 825c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes delete jni_env_; 82602b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes jni_env_ = NULL; 82702b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 82802b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes SetState(Thread::kTerminated); 82985d1545e985ac689db4bad7849880e843707c862Elliott Hughes 83085d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete wait_cond_; 83185d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete wait_mutex_; 83285d1545e985ac689db4bad7849880e843707c862Elliott Hughes 83385d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete long_jump_context_; 834475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes 835475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes delete debug_invoke_req_; 836c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes} 837c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes 838accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughesvoid Thread::HandleUncaughtExceptions() { 839accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes if (!IsExceptionPending()) { 840accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes return; 841accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes } 842accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 843accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Get and clear the exception. 844accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Object* exception = GetException(); 845accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes ClearException(); 846accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 847accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // If the thread has its own handler, use that. 848accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Object* handler = gThread_uncaughtHandler->GetObject(peer_); 849accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes if (handler == NULL) { 850accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Otherwise use the thread group's default handler. 851a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes handler = GetThreadGroup(); 852accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes } 853accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 854accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Call the handler. 855accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Method* m = handler->GetClass()->FindVirtualMethodForVirtualOrInterface(gUncaughtExceptionHandler_uncaughtException); 856accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Object* args[2]; 857accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes args[0] = peer_; 858accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes args[1] = exception; 859accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes m->Invoke(this, handler, reinterpret_cast<byte*>(&args), NULL); 860accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 861accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // If the handler threw, clear that exception too. 862accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes ClearException(); 863accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 8644514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom 865a215526d5c789cbef0f81a1f9aba22541a841ccaElliott HughesObject* Thread::GetThreadGroup() const { 866a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes return gThread_group->GetObject(peer_); 867a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes} 868a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes 8694514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstromvoid Thread::RemoveFromThreadGroup() { 8704514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom // this.group.removeThread(this); 8714514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom // group can be null if we're in the compiler or a test. 872a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes Object* group = GetThreadGroup(); 8734514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom if (group != NULL) { 8744514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom Method* m = group->GetClass()->FindVirtualMethodForVirtualOrInterface(gThreadGroup_removeThread); 8754514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom Object* args = peer_; 8764514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom m->Invoke(this, group, reinterpret_cast<byte*>(&args), NULL); 8774514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom } 8784514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom} 879accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 880408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogerssize_t Thread::NumSirtReferences() { 881a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers size_t count = 0; 88240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { 883a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers count += cur->NumberOfReferences(); 884a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 885a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return count; 886a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers} 887a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers 888408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogersbool Thread::SirtContains(jobject obj) { 889408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers Object** sirt_entry = reinterpret_cast<Object**>(obj); 89040381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { 89140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom if (cur->Contains(sirt_entry)) { 892a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return true; 893a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 894a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 895a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return false; 896a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers} 897a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers 8988dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liaovoid Thread::SirtVisitRoots(Heap::RootVisitor* visitor, void* arg) { 89940381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { 9008dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao size_t num_refs = cur->NumberOfReferences(); 9018dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao for (size_t j = 0; j < num_refs; j++) { 90240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom Object* object = cur->GetReference(j); 9035e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom if (object != NULL) { 9045e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom visitor(object, arg); 9055e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom } 9068dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao } 9078dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao } 9088dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao} 9098dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 910408f79aeb676251ba35667a64e86c20638d7cb0bIan RogersObject* Thread::DecodeJObject(jobject obj) { 9110cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers DCHECK(CanAccessDirectReferences()); 912408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (obj == NULL) { 913408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return NULL; 914408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 915408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectRef ref = reinterpret_cast<IndirectRef>(obj); 916408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectRefKind kind = GetIndirectRefKind(ref); 917408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers Object* result; 918408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers switch (kind) { 919408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kLocal: 920408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 92169f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughes IndirectReferenceTable& locals = jni_env_->locals; 922cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(locals.Get(ref)); 923408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 924408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 925408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kGlobal: 926408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 927408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers JavaVMExt* vm = Runtime::Current()->GetJavaVM(); 928408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectReferenceTable& globals = vm->globals; 929408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers MutexLock mu(vm->globals_lock); 930cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(globals.Get(ref)); 931408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 932408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 933408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kWeakGlobal: 934408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 935408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers JavaVMExt* vm = Runtime::Current()->GetJavaVM(); 936408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectReferenceTable& weak_globals = vm->weak_globals; 937408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers MutexLock mu(vm->weak_globals_lock); 938cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(weak_globals.Get(ref)); 939408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (result == kClearedJniWeakGlobal) { 940408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // This is a special case where it's okay to return NULL. 941408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return NULL; 942408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 943408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 944408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 945408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kSirtOrInvalid: 946408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers default: 947408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // TODO: make stack indirect reference table lookup more efficient 948408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Check if this is a local reference in the SIRT 949408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (SirtContains(obj)) { 9500cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers result = *reinterpret_cast<Object**>(obj); // Read from SIRT 951c5bfa8f49d8548d7c685a99b411311ef56bedffaElliott Hughes } else if (jni_env_->work_around_app_jni_bugs) { 952408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Assume an invalid local reference is actually a direct pointer. 953408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers result = reinterpret_cast<Object*>(obj); 954408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } else { 955a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes result = kInvalidIndirectRefObject; 956408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 957408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 958408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 959408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (result == NULL) { 960a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes LOG(ERROR) << "JNI ERROR (app bug): use of deleted " << kind << ": " << obj; 961a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes JniAbort(NULL); 962a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes } else { 963a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes if (result != kInvalidIndirectRefObject) { 964a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes Heap::VerifyObject(result); 965a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes } 966408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 967408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return result; 968408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers} 969408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 9709b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liaoclass CountStackDepthVisitor : public Thread::StackVisitor { 9719b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 97229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes CountStackDepthVisitor() : depth_(0), skip_depth_(0), skipping_(true) {} 973d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 97429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes virtual void VisitFrame(const Frame& frame, uintptr_t pc) { 97529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // We want to skip frames up to and including the exception's constructor. 9769086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // Note we also skip the frame if it doesn't have a method (namely the callee 9779086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // save frame) 97825c3325bf95036bf325fc7cb21b4fd6d40282857Brian Carlstrom DCHECK(gThrowable != NULL); 9799086572fa809d1a19d886b467e4da3ce42016982Ian Rogers if (skipping_ && frame.HasMethod() && !gThrowable->IsAssignableFrom(frame.GetMethod()->GetDeclaringClass())) { 98029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes skipping_ = false; 98129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 98229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (!skipping_) { 98329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes ++depth_; 98429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } else { 98529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes ++skip_depth_; 98629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 9879b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9889b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 9899b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao int GetDepth() const { 990aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return depth_; 9919b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9929b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 99329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int GetSkipDepth() const { 99429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes return skip_depth_; 99529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 99629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 9979b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao private: 998aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t depth_; 99929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes uint32_t skip_depth_; 100029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes bool skipping_; 10019b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao}; 10029b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1003aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogersclass BuildInternalStackTraceVisitor : public Thread::StackVisitor { 10049b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 100529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes explicit BuildInternalStackTraceVisitor(int depth, int skip_depth, ScopedJniThreadState& ts) 1006726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes : skip_depth_(skip_depth), count_(0), pc_trace_(NULL), method_trace_(NULL), local_ref_(NULL) { 1007aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Allocate method trace with an extra slot that will hold the PC trace 100801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes method_trace_ = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(depth + 1); 1009726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (method_trace_ == NULL) { 1010726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes return; 1011726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 1012aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Register a local reference as IntArray::Alloc may trigger GC 1013aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers local_ref_ = AddLocalReference<jobject>(ts.Env(), method_trace_); 1014aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers pc_trace_ = IntArray::Alloc(depth); 1015726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (pc_trace_ == NULL) { 1016726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes return; 1017726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 1018aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#ifdef MOVING_GARBAGE_COLLECTOR 1019aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Re-read after potential GC 1020aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace = Decode<ObjectArray<Object>*>(ts.Env(), local_ref_); 1021aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#endif 1022aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Save PC trace in last element of method trace, also places it into the 1023aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // object graph. 1024aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace_->Set(depth, pc_trace_); 10259b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 10269b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1027aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers virtual ~BuildInternalStackTraceVisitor() {} 10289b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1029bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers virtual void VisitFrame(const Frame& frame, uintptr_t pc) { 1030726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (method_trace_ == NULL || pc_trace_ == NULL) { 1031726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes return; // We're probably trying to fillInStackTrace for an OutOfMemoryError. 1032726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 103329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (skip_depth_ > 0) { 103429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes skip_depth_--; 103529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes return; 103629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 1037aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace_->Set(count_, frame.GetMethod()); 1038bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers pc_trace_->Set(count_, pc); 1039aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ++count_; 10409b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 10419b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1042aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers jobject GetInternalStackTrace() const { 1043aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return local_ref_; 10449b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 10459b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 10469b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao private: 104729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // How many more frames to skip. 104829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int32_t skip_depth_; 1049aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Current position down stack trace 1050aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t count_; 1051aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Array of return PC values 1052aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers IntArray* pc_trace_; 1053aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // An array of the methods on the stack, the last entry is a reference to the 1054aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // PC trace 1055aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ObjectArray<Object>* method_trace_; 1056aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Local indirect reference table entry for method trace 1057aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers jobject local_ref_; 10589b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao}; 10599b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1060a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes// TODO: remove this. 1061a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughesuintptr_t ManglePc(uintptr_t pc) { 1062a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // Move the PC back 2 bytes as a call will frequently terminate the 1063a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // decoding of a particular instruction and we want to make sure we 1064a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // get the Dex PC of the instruction with the call and not the 1065a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // instruction following. 1066a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes if (pc > 0) { pc -= 2; } 1067a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes return pc; 1068a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes} 1069a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes 1070a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes// TODO: remove this. 1071a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughesuintptr_t DemanglePc(uintptr_t pc) { 1072a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // Revert mangling for the case where we need the PC to return to the upcall 1073a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes return pc + 2; 1074a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes} 1075f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers 107640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstromvoid Thread::PushSirt(StackIndirectReferenceTable* sirt) { 107740381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom sirt->SetLink(top_sirt_); 107840381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom top_sirt_ = sirt; 107940381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom} 108040381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom 108140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian CarlstromStackIndirectReferenceTable* Thread::PopSirt() { 108240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom CHECK(top_sirt_ != NULL); 108340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom StackIndirectReferenceTable* sirt = top_sirt_; 108440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom top_sirt_ = top_sirt_->GetLink(); 108540381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom return sirt; 108640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom} 108740381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom 1088aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogersvoid Thread::WalkStack(StackVisitor* visitor) const { 1089d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Frame frame = GetTopOfStack(); 1090a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes uintptr_t pc = ManglePc(top_of_managed_stack_pc_); 10919b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao // TODO: enable this CHECK after native_to_managed_record_ is initialized during startup. 10929b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao // CHECK(native_to_managed_record_ != NULL); 10939b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao NativeToManagedRecord* record = native_to_managed_record_; 10944417536522fd2a9d8215d8672331984769c9520bShih-wei Liao 1095bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers while (frame.GetSP() != 0) { 10969b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao for ( ; frame.GetMethod() != 0; frame.Next()) { 10973320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom // DCHECK(frame.GetMethod()->IsWithinCode(pc)); // TODO: restore IsWithinCode 10983ddac99d4dc6a036fac59d8f0bdc664ef619fb04Shih-wei Liao visitor->VisitFrame(frame, pc); 1099a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes pc = ManglePc(frame.GetReturnPC()); 11009b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 11019b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao if (record == NULL) { 11029b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao break; 11039b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 1104bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // last_tos should return Frame instead of sp? 1105ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers frame.SetSP(reinterpret_cast<Method**>(record->last_top_of_managed_stack_)); 1106a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes pc = ManglePc(record->last_top_of_managed_stack_pc_); 1107bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers record = record->link_; 1108bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1109bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers} 1110bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 111167375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogersvoid Thread::WalkStackUntilUpCall(StackVisitor* visitor, bool include_upcall) const { 1112bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Frame frame = GetTopOfStack(); 1113a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes uintptr_t pc = ManglePc(top_of_managed_stack_pc_); 1114bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1115bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (frame.GetSP() != 0) { 1116bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers for ( ; frame.GetMethod() != 0; frame.Next()) { 11173320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom // DCHECK(frame.GetMethod()->IsWithinCode(pc)); // TODO: restore IsWithinCode 11183ddac99d4dc6a036fac59d8f0bdc664ef619fb04Shih-wei Liao visitor->VisitFrame(frame, pc); 1119a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes pc = ManglePc(frame.GetReturnPC()); 1120bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 112167375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers if (include_upcall) { 112267375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers visitor->VisitFrame(frame, pc); 112367375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers } 112455df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao } 112555df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao} 112655df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 112701158d7a57c8321370667a6045220237d16e0da8Elliott Hughesjobject Thread::CreateInternalStackTrace(JNIEnv* env) const { 1128aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Compute depth of stack 11299b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao CountStackDepthVisitor count_visitor; 11309b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao WalkStack(&count_visitor); 11319b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao int32_t depth = count_visitor.GetDepth(); 113229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int32_t skip_depth = count_visitor.GetSkipDepth(); 11334417536522fd2a9d8215d8672331984769c9520bShih-wei Liao 1134aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Transition into runnable state to work on Object*/Array* 113501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes ScopedJniThreadState ts(env); 1136aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 1137aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Build internal stack trace 113829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes BuildInternalStackTraceVisitor build_trace_visitor(depth, skip_depth, ts); 11399b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao WalkStack(&build_trace_visitor); 11404417536522fd2a9d8215d8672331984769c9520bShih-wei Liao 1141aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return build_trace_visitor.GetInternalStackTrace(); 1142aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers} 1143aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 114401158d7a57c8321370667a6045220237d16e0da8Elliott HughesjobjectArray Thread::InternalStackTraceToStackTraceElementArray(JNIEnv* env, jobject internal, 114501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobjectArray output_array, int* stack_depth) { 1146aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Transition into runnable state to work on Object*/Array* 1147aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ScopedJniThreadState ts(env); 1148aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Decode the internal stack trace into the depth, method trace and PC trace 1149aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ObjectArray<Object>* method_trace = 1150aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); 11519074b99bfbd5419e340f138a018b673ce71f77e8Ian Rogers int32_t depth = method_trace->GetLength() - 1; 1152aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); 1153aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 1154aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1155aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 115601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobjectArray result; 115701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes ObjectArray<StackTraceElement>* java_traces; 115801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes if (output_array != NULL) { 115901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Reuse the array we were given. 116001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes result = output_array; 116101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes java_traces = reinterpret_cast<ObjectArray<StackTraceElement>*>(Decode<Array*>(env, 116201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes output_array)); 116301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // ...adjusting the number of frames we'll write to not exceed the array length. 116401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes depth = std::min(depth, java_traces->GetLength()); 116501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } else { 116601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Create java_trace array and place in local reference table 116701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes java_traces = class_linker->AllocStackTraceElementArray(depth); 116830646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (java_traces == NULL) { 116930646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return NULL; 117030646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 117101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes result = AddLocalReference<jobjectArray>(ts.Env(), java_traces); 117201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } 117301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 117401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes if (stack_depth != NULL) { 117501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes *stack_depth = depth; 117601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } 117755df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 11786d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers MethodHelper mh; 11799b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao for (int32_t i = 0; i < depth; ++i) { 1180aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Prepare parameters for StackTraceElement(String cls, String method, String file, int line) 1181aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers Method* method = down_cast<Method*>(method_trace->Get(i)); 11826d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers mh.ChangeMethod(method); 1183aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t native_pc = pc_trace->Get(i); 11846d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers int32_t line_number = mh.GetLineNumFromNativePC(native_pc); 1185aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Allocate element, potentially triggering GC 118640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom // TODO: reuse class_name_object via Class::name_? 11876d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers std::string class_name(PrettyDescriptor(mh.GetDeclaringClassDescriptor())); 118840381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom SirtRef<String> class_name_object(String::AllocFromModifiedUtf8(class_name.c_str())); 118940381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom if (class_name_object.get() == NULL) { 119040381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom return NULL; 119140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom } 11926d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers SirtRef<String> method_name_object(String::AllocFromModifiedUtf8(mh.GetName())); 11936d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers if (method_name_object.get() == NULL) { 11946d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers return NULL; 11956d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers } 11966d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers SirtRef<String> 11976d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers source_name_object(String::AllocFromModifiedUtf8(mh.GetDeclaringClassSourceFile())); 11986d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers if (source_name_object.get() == NULL) { 11996d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers return NULL; 12006d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers } 120140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom StackTraceElement* obj = StackTraceElement::Alloc(class_name_object.get(), 12026d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers method_name_object.get(), 12036d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers source_name_object.get(), 120440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom line_number); 120530646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (obj == NULL) { 120630646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return NULL; 120730646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1208aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#ifdef MOVING_GARBAGE_COLLECTOR 1209aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Re-read after potential GC 1210aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers java_traces = Decode<ObjectArray<Object>*>(ts.Env(), result); 1211aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace = down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); 1212aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); 1213aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#endif 121455df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao java_traces->Set(i, obj); 121555df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao } 1216aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return result; 121755df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao} 121855df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 12195cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughesvoid Thread::ThrowNewExceptionF(const char* exception_class_descriptor, const char* fmt, ...) { 1220a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_list args; 1221a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_start(args, fmt); 12224a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes ThrowNewExceptionV(exception_class_descriptor, fmt, args); 1223a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_end(args); 12244a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes} 12254a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes 12264a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughesvoid Thread::ThrowNewExceptionV(const char* exception_class_descriptor, const char* fmt, va_list ap) { 12274a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes std::string msg; 12284a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes StringAppendV(&msg, fmt, ap); 12295cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes ThrowNewException(exception_class_descriptor, msg.c_str()); 12305cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes} 123137f7a40f6789bb287f287a9af00777af9d6428eeElliott Hughes 12325cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughesvoid Thread::ThrowNewException(const char* exception_class_descriptor, const char* msg) { 1233e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes // Convert "Ljava/lang/Exception;" into JNI-style "java/lang/Exception". 12340cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers CHECK_EQ('L', exception_class_descriptor[0]); 1235e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes std::string descriptor(exception_class_descriptor + 1); 12360cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers CHECK_EQ(';', descriptor[descriptor.length() - 1]); 1237e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes descriptor.erase(descriptor.length() - 1); 1238e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes 1239e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes JNIEnv* env = GetJniEnv(); 1240726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jclass> exception_class(env, env->FindClass(descriptor.c_str())); 124130646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (exception_class.get() == NULL) { 124230646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI FindClass failed: " 124330646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes << PrettyTypeOf(GetException()); 124430646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes CHECK(IsExceptionPending()); 124530646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return; 124630646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1247726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes int rc = env->ThrowNew(exception_class.get(), msg); 124830646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (rc != JNI_OK) { 124930646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI ThrowNew failed: " 125030646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes << PrettyTypeOf(GetException()); 125130646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes CHECK(IsExceptionPending()); 125230646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return; 125330646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1254a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes} 1255a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes 1256418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughesvoid Thread::ThrowOutOfMemoryError(Class* c, size_t byte_count) { 12572ced6a534157d5d963693346904389c19775d2daElliott Hughes std::string msg(StringPrintf("Failed to allocate a %zd-byte %s", byte_count, 12586d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers PrettyDescriptor(c).c_str())); 12592ced6a534157d5d963693346904389c19775d2daElliott Hughes ThrowOutOfMemoryError(msg.c_str()); 12602ced6a534157d5d963693346904389c19775d2daElliott Hughes} 12612ced6a534157d5d963693346904389c19775d2daElliott Hughes 12622ced6a534157d5d963693346904389c19775d2daElliott Hughesvoid Thread::ThrowOutOfMemoryError(const char* msg) { 12632ced6a534157d5d963693346904389c19775d2daElliott Hughes LOG(ERROR) << StringPrintf("Throwing OutOfMemoryError \"%s\"%s", 12642ced6a534157d5d963693346904389c19775d2daElliott Hughes msg, (throwing_OutOfMemoryError_ ? " (recursive case)" : "")); 1265726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (!throwing_OutOfMemoryError_) { 1266726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_ = true; 1267accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes ThrowNewException("Ljava/lang/OutOfMemoryError;", NULL); 1268418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes } else { 1269726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes SetException(pre_allocated_OutOfMemoryError_); 1270418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes } 1271726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_ = false; 127279082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes} 127379082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes 1274accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1275498508c1187dc07d3eae5476784cde20f5224d93Elliott HughesThread* Thread::CurrentFromGdb() { 1276accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes return Thread::Current(); 1277accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 1278accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1279accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughesvoid Thread::DumpFromGdb() const { 12806b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom std::ostringstream ss; 12816b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom Dump(ss); 12826b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom std::string str = ss.str(); 12836b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom // log to stderr for debugging command line processes 12846b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom std::cerr << str; 12856b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom#ifdef HAVE_ANDROID_OS 12866b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom // log to logcat for debugging frameworks processes 12876b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom LOG(INFO) << str; 12886b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom#endif 1289accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 1290accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1291bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogersclass CatchBlockStackVisitor : public Thread::StackVisitor { 1292bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers public: 1293bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers CatchBlockStackVisitor(Class* to_find, Context* ljc) 129467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers : found_(false), to_find_(to_find), long_jump_context_(ljc), native_method_count_(0) { 129567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers#ifndef NDEBUG 129667375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_pc_ = 0xEBADC0DE; 129767375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_frame_.SetSP(reinterpret_cast<Method**>(0xEBADF00D)); 129867375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers#endif 129967375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers } 1300bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1301bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers virtual void VisitFrame(const Frame& fr, uintptr_t pc) { 1302bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (!found_) { 1303bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Method* method = fr.GetMethod(); 130467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers if (method == NULL) { 130567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // This is the upcall, we remember the frame and last_pc so that we may 130667375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // long jump to them 1307a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes handler_pc_ = DemanglePc(pc); 130867375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_frame_ = fr; 130967375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers return; 131067375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers } 131167375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uint32_t dex_pc = DexFile::kDexNoIndex; 13123320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom if (method->IsCalleeSaveMethod()) { 13139086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // ignore callee save method 13149086572fa809d1a19d886b467e4da3ce42016982Ian Rogers } else if (method->IsNative()) { 13159086572fa809d1a19d886b467e4da3ce42016982Ian Rogers native_method_count_++; 13169086572fa809d1a19d886b467e4da3ce42016982Ian Rogers } else { 13179086572fa809d1a19d886b467e4da3ce42016982Ian Rogers dex_pc = method->ToDexPC(pc); 1318bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1319bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (dex_pc != DexFile::kDexNoIndex) { 1320bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers uint32_t found_dex_pc = method->FindCatchBlock(to_find_, dex_pc); 1321bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (found_dex_pc != DexFile::kDexNoIndex) { 1322bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers found_ = true; 132367375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_pc_ = method->ToNativePC(found_dex_pc); 132467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_frame_ = fr; 1325bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1326bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1327bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (!found_) { 1328bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Caller may be handler, fill in callee saves in context 1329bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers long_jump_context_->FillCalleeSaves(fr); 1330bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 13311a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 13321a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 13331a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 1334bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Did we find a catch block yet? 1335bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers bool found_; 1336bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // The type of the exception catch block to find 1337bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Class* to_find_; 1338bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Frame with found handler or last frame if no handler found 1339bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Frame handler_frame_; 134067375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // PC to branch to for the handler 134167375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uintptr_t handler_pc_; 1342bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Context that will be the target of the long jump 1343bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Context* long_jump_context_; 134467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // Number of native methods passed in crawl (equates to number of SIRTs to pop) 134567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uint32_t native_method_count_; 1346bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers}; 1347bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1348ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogersvoid Thread::DeliverException() { 134928ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers const bool kDebugExceptionDelivery = false; 1350d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Throwable* exception = GetException(); // Get exception from thread 1351ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers CHECK(exception != NULL); 135228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers // Don't leave exception visible while we try to find the handler, which may cause class 1353d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes // resolution. 135428ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers ClearException(); 135528ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (kDebugExceptionDelivery) { 135628ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception) << std::endl); 135728ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 1358bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1359bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Context* long_jump_context = GetLongJumpContext(); 1360bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers CatchBlockStackVisitor catch_finder(exception->GetClass(), long_jump_context); 136167375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers WalkStackUntilUpCall(&catch_finder, true); 1362bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1363d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Method** sp; 1364d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes uintptr_t throw_native_pc; 1365d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Method* throw_method = GetCurrentMethod(&throw_native_pc, &sp); 1366d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes uintptr_t catch_native_pc = catch_finder.handler_pc_; 1367d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Method* catch_method = catch_finder.handler_frame_.GetMethod(); 1368d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Dbg::PostException(sp, throw_method, throw_native_pc, catch_method, catch_native_pc, exception); 1369d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes 137028ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (kDebugExceptionDelivery) { 1371d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes if (catch_method == NULL) { 137228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers LOG(INFO) << "Handler is upcall"; 137328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } else { 137428ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 137528ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers const DexFile& dex_file = 1376d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes class_linker->FindDexFile(catch_method->GetDeclaringClass()->GetDexCache()); 1377d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes int line_number = dex_file.GetLineNumFromPC(catch_method, 1378d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes catch_method->ToDexPC(catch_finder.handler_pc_)); 1379d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes LOG(INFO) << "Handler: " << PrettyMethod(catch_method) << " (line: " << line_number << ")"; 138028ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 138128ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 138228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers SetException(exception); 1383d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes long_jump_context->SetSP(reinterpret_cast<uintptr_t>(catch_finder.handler_frame_.GetSP())); 138467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers long_jump_context->SetPC(catch_finder.handler_pc_); 1385bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers long_jump_context->DoLongJump(); 1386bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers} 1387bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1388bdb0391258abc54bf77c676e36847d28a783bfe5Ian RogersContext* Thread::GetLongJumpContext() { 138985d1545e985ac689db4bad7849880e843707c862Elliott Hughes Context* result = long_jump_context_; 1390bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (result == NULL) { 1391bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers result = Context::Create(); 139285d1545e985ac689db4bad7849880e843707c862Elliott Hughes long_jump_context_ = result; 13931a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 1394bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers return result; 13951a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao} 13961a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 1397d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott HughesMethod* Thread::GetCurrentMethod(uintptr_t* pc, Method*** sp) const { 1398d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Frame f = top_of_managed_stack_; 1399d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Method* m = f.GetMethod(); 14009fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // We use JNI internally for exception throwing, so it's possible to arrive 14019fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // here via a "FromCode" function, in which case there's a synthetic 14029fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // callee-save method at the top of the stack. These shouldn't be user-visible, 14039fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // so if we find one, skip it and return the compiled method underneath. 140433dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao if (m != NULL && m->IsCalleeSaveMethod()) { 14059fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes f.Next(); 14069fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes m = f.GetMethod(); 14079fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes } 1408d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes if (pc != NULL) { 1409d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes *pc = ManglePc(f.GetReturnPC()); 141033dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao } 1411d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes if (sp != NULL) { 1412d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes *sp = f.GetSP(); 1413d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes } 1414d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes return m; 141533dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao} 141633dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao 14175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesbool Thread::HoldsLock(Object* object) { 14185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (object == NULL) { 14195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 14205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 142124a3c2e9924e8765c4a9b4d383cb8f3b922f9c9fBrian Carlstrom return object->GetThinLockId() == thin_lock_id_; 14225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 14235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1424038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesbool Thread::IsDaemon() { 1425038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes return gThread_daemon->GetBoolean(peer_); 1426038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes} 1427038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 1428d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogersclass ReferenceMapVisitor : public Thread::StackVisitor { 1429d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers public: 1430d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers ReferenceMapVisitor(Context* context, Heap::RootVisitor* root_visitor, void* arg) : 1431d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers context_(context), root_visitor_(root_visitor), arg_(arg) { 1432d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1433d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1434d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers void VisitFrame(const Frame& frame, uintptr_t pc) { 1435d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Method* m = frame.GetMethod(); 14366a4be3a6226cec645cf905dd352e44f7968a7fa4Brian Carlstrom if (false) { 14376a4be3a6226cec645cf905dd352e44f7968a7fa4Brian Carlstrom LOG(INFO) << "Visiting stack roots in " << PrettyMethod(m, false) 14380dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom << StringPrintf("@ PC:%04x", m->ToDexPC(pc)); 14390dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom 14406a4be3a6226cec645cf905dd352e44f7968a7fa4Brian Carlstrom } 1441d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Process register map (which native and callee save methods don't have) 14423891c775503b1113a918180d9ecdaa79e92c69c6Ian Rogers if (!m->IsNative() && !m->IsCalleeSaveMethod() && !m->IsProxyMethod()) { 1443d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers verifier::PcToReferenceMap map(m); 1444d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers const uint8_t* reg_bitmap = map.FindBitMap(m->ToDexPC(pc)); 1445d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers CHECK(reg_bitmap != NULL); 144668fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes const VmapTable vmap_table(m->GetVmapTableRaw()); 14476d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers const art::DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); 14486d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions? 14496d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers uint32_t core_spills = m->GetCoreSpillMask(); 14506d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers uint32_t fp_spills = m->GetFpSpillMask(); 14516d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers size_t frame_size = m->GetFrameSizeInBytes(); 14520dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom // For all dex registers in the bitmap 14536d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers size_t num_regs = std::min(map.RegWidth() * 8, 14546d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers static_cast<size_t>(code_item->registers_size_)); 14550dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom for (size_t reg = 0; reg < num_regs; ++reg) { 1456d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Does this register hold a reference? 1457d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers if (TestBitmap(reg, reg_bitmap)) { 145868fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes uint32_t vmap_offset; 1459d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Object* ref; 146068fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes if (vmap_table.IsInContext(reg, vmap_offset)) { 1461d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Compute the register we need to load from the context 1462d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers uint32_t spill_mask = m->GetCoreSpillMask(); 14630dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom CHECK_LT(vmap_offset, static_cast<uint32_t>(__builtin_popcount(spill_mask))); 1464f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers uint32_t matches = 0; 1465f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers uint32_t spill_shifts = 0; 1466f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers while (matches != (vmap_offset + 1)) { 14670dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom DCHECK_NE(spill_mask, 0u); 1468f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers matches += spill_mask & 1; // Add 1 if the low bit is set 1469f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_mask >>= 1; 1470f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_shifts++; 1471d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1472f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_shifts--; // wind back one as we want the last match 1473f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers ref = reinterpret_cast<Object*>(context_->GetGPR(spill_shifts)); 1474d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } else { 14756d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers ref = reinterpret_cast<Object*>(frame.GetVReg(code_item, core_spills, fp_spills, 14766d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers frame_size, reg)); 1477d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 14784f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao if (ref != NULL) { 14794f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao root_visitor_(ref, arg_); 14804f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao } 1481d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1482d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1483d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1484d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers context_->FillCalleeSaves(frame); 1485d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1486d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1487d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers private: 1488d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers bool TestBitmap(int reg, const uint8_t* reg_vector) { 1489d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers return ((reg_vector[reg / 8] >> (reg % 8)) & 0x01) != 0; 1490d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1491d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1492d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Context used to build up picture of callee saves 1493d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Context* context_; 1494d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Call-back when we visit a root 1495d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Heap::RootVisitor* root_visitor_; 1496d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Argument to call-back 1497d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers void* arg_; 1498d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers}; 1499d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1500d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogersvoid Thread::VisitRoots(Heap::RootVisitor* visitor, void* arg) { 1501d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (exception_ != NULL) { 1502d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes visitor(exception_, arg); 1503d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 1504d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (peer_ != NULL) { 1505d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes visitor(peer_, arg); 1506d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 1507726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (pre_allocated_OutOfMemoryError_ != NULL) { 1508726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes visitor(pre_allocated_OutOfMemoryError_, arg); 1509726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 151040381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom if (class_loader_override_ != NULL) { 151140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom visitor(class_loader_override_, arg); 151240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom } 1513410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes jni_env_->locals.VisitRoots(visitor, arg); 1514410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes jni_env_->monitors.VisitRoots(visitor, arg); 15158dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 15168dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao SirtVisitRoots(visitor, arg); 15178dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 1518d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Cheat and steal the long jump context. Assume that we are not doing a GC during exception 1519d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // delivery. 1520d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Context* context = GetLongJumpContext(); 1521d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Visit roots on this thread's stack 1522d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers ReferenceMapVisitor mapper(context, visitor, arg); 1523d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers WalkStack(&mapper); 1524410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes} 1525410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes 1526b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogersstatic const char* kStateNames[] = { 152793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "Terminated", 1528b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers "Runnable", 152993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "TimedWaiting", 1530b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers "Blocked", 1531b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers "Waiting", 153293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "Initializing", 153393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "Starting", 1534b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers "Native", 153593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "VmWait", 153693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "Suspended", 1537b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers}; 1538b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogersstd::ostream& operator<<(std::ostream& os, const Thread::State& state) { 15398e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes int32_t int_state = static_cast<int32_t>(state); 154093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes if (state >= Thread::kTerminated && state <= Thread::kSuspended) { 154193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes os << kStateNames[int_state]; 1542b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers } else { 154393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes os << "State[" << int_state << "]"; 1544b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers } 1545b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers return os; 1546b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers} 1547b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers 1548330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughesstd::ostream& operator<<(std::ostream& os, const Thread& thread) { 154947179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes os << "Thread["; 155047179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes if (thread.GetThinLockId() != 0) { 155147179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes // If we're in kStarting, we won't have a thin lock id or tid yet. 155247179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes os << thread.GetThinLockId() 155347179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes << ",tid=" << thread.GetTid() << ','; 155447179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes } 155547179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes os << thread.GetState() 155647179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes << ",Thread*=" << &thread 155747179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes << ",Object*=" << thread.GetPeer() 15588daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes << "]"; 1559330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes return os; 1560330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes} 1561330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes 15628daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} // namespace art 1563