thread.cc revision e0918556e7551de638870dcad3f2023f94f85a50
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" 38578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "runtime.h" 395433072f589b61413e042eddf76e8190a048f71dbuzbee#include "runtime_support.h" 40726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes#include "ScopedLocalRef.h" 41aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#include "scoped_jni_thread_state.h" 4268e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack.h" 4368e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack_indirect_reference_table.h" 448daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "thread_list.h" 45a09576416788b916095739e43a16917e7948f3a4Elliott Hughes#include "utils.h" 46b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 47b557353b22c728eecbd1c68593b482622c7782a8Carl Shapironamespace art { 48b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 49b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiropthread_key_t Thread::pthread_key_self_; 50b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 518e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesstatic Class* gThreadLock = NULL; 5229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Class* gThrowable = NULL; 53038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_daemon = NULL; 54038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_group = NULL; 55038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_lock = NULL; 56038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_name = NULL; 57038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_priority = NULL; 5829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Field* gThread_uncaughtHandler = NULL; 59038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_vmData = NULL; 60038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThreadGroup_name = NULL; 618e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesstatic Field* gThreadLock_thread = NULL; 62038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Method* gThread_run = NULL; 6329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Method* gThreadGroup_removeThread = NULL; 6429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Method* gUncaughtExceptionHandler_uncaughtException = NULL; 65038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 663ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbeevoid Thread::InitFunctionPointers() { 675433072f589b61413e042eddf76e8190a048f71dbuzbee#if defined(__arm__) 685433072f589b61413e042eddf76e8190a048f71dbuzbee pShlLong = art_shl_long; 695433072f589b61413e042eddf76e8190a048f71dbuzbee pShrLong = art_shr_long; 705433072f589b61413e042eddf76e8190a048f71dbuzbee pUshrLong = art_ushr_long; 717b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pIdiv = __aeabi_idiv; 727b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pIdivmod = __aeabi_idivmod; 737b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pI2f = __aeabi_i2f; 747b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pF2iz = __aeabi_f2iz; 757b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pD2f = __aeabi_d2f; 767b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pF2d = __aeabi_f2d; 777b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pD2iz = __aeabi_d2iz; 787b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pL2f = __aeabi_l2f; 797b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pL2d = __aeabi_l2d; 807b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFadd = __aeabi_fadd; 817b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFsub = __aeabi_fsub; 827b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFdiv = __aeabi_fdiv; 837b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFmul = __aeabi_fmul; 847b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFmodf = fmodf; 857b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pDadd = __aeabi_dadd; 867b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pDsub = __aeabi_dsub; 877b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pDdiv = __aeabi_ddiv; 887b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pDmul = __aeabi_dmul; 897b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFmod = fmod; 907b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pLdivmod = __aeabi_ldivmod; 91439c4fa0db980fb19e4a585723a64a3461e4c278buzbee pLmul = __aeabi_lmul; 92b408de744566a5c5a80be1ba7f5c88407e816945Elliott Hughes pAllocArrayFromCode = art_alloc_array_from_code; 936fd03fb67e4628689f0abf34edeacc0e35b6295eBrian Carlstrom pAllocObjectFromCode = art_alloc_object_from_code; 94cc4540ece917ef4b5c81ee684e3294e47e49ff5bbuzbee pAllocObjectFromCodeSlowPath = art_alloc_object_from_code_slow_path; 95e51a511ccee3f3c0120807321bcc160fcaa664beIan Rogers pCanPutArrayElementFromCode = art_can_put_array_element_from_code; 96b408de744566a5c5a80be1ba7f5c88407e816945Elliott Hughes pCheckAndAllocArrayFromCode = art_check_and_alloc_array_from_code; 97ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pCheckCastFromCode = art_check_cast_from_code; 98ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pFindInstanceFieldFromCode = art_find_instance_field_from_code; 99ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pGet32Static = art_get32_static_from_code; 100ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pGet64Static = art_get64_static_from_code; 101ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pGetObjStatic = art_get_obj_static_from_code; 102ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pHandleFillArrayDataFromCode = art_handle_fill_data_from_code; 103cbba6ac9bf9a6c630a7aafae6d8767b5ddbb6fd5Ian Rogers pInitializeStaticStorage = art_initialize_static_storage_from_code; 1044a3164faefd255b1c1e911e7ad7c3d57749caaf6buzbee pInvokeInterfaceTrampoline = art_invoke_interface_trampoline; 1056fd03fb67e4628689f0abf34edeacc0e35b6295eBrian Carlstrom pLockObjectFromCode = art_lock_object_from_code; 1066fd03fb67e4628689f0abf34edeacc0e35b6295eBrian Carlstrom pObjectInit = art_object_init_from_code; 1076fd03fb67e4628689f0abf34edeacc0e35b6295eBrian Carlstrom pResolveStringFromCode = art_resolve_string_from_code; 108ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pSet32Static = art_set32_static_from_code; 109ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pSet64Static = art_set64_static_from_code; 110ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers pSetObjStatic = art_set_obj_static_from_code; 1116fd03fb67e4628689f0abf34edeacc0e35b6295eBrian Carlstrom pTestSuspendFromCode = art_test_suspend; 112ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pThrowArrayBoundsFromCode = art_throw_array_bounds_from_code; 113ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pThrowDivZeroFromCode = art_throw_div_zero_from_code; 114c0c8dc8e677957d167066a2d06973126f108a6aaIan Rogers pThrowNegArraySizeFromCode = art_throw_neg_array_size_from_code; 115c0c8dc8e677957d167066a2d06973126f108a6aaIan Rogers pThrowNoSuchMethodFromCode = art_throw_no_such_method_from_code; 116ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pThrowNullPointerFromCode = art_throw_null_pointer_exception_from_code; 117932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers pThrowStackOverflowFromCode = art_throw_stack_overflow_from_code; 118c0c8dc8e677957d167066a2d06973126f108a6aaIan Rogers pThrowVerificationErrorFromCode = art_throw_verification_error_from_code; 119ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pUnlockObjectFromCode = art_unlock_object_from_code; 12067375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers#endif 121c396efc1baec875160afe34a3e65c46cce2bd72fbuzbee pF2l = F2L; 122c396efc1baec875160afe34a3e65c46cce2bd72fbuzbee pD2l = D2L; 1233ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee pMemcpy = memcpy; 1244a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pCheckSuspendFromCode = CheckSuspendFromCode; 1254a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pDebugMe = DebugMe; 1264a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pDecodeJObjectInThread = DecodeJObjectInThread; 1274a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pDeliverException = art_deliver_exception_from_code; 1284a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pFindNativeMethod = FindNativeMethod; 1291b4c85959b3d9a4a33bc2160c46c1bbde67350c7buzbee pInitializeTypeFromCode = InitializeTypeFromCode; 130caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers pInstanceofNonTrivialFromCode = IsAssignableFromCode; 1314a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pResolveMethodFromCode = ResolveMethodFromCode; 1324a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pThrowAbstractMethodErrorFromCode = ThrowAbstractMethodErrorFromCode; 1334a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers pUnresolvedDirectMethodTrampolineFromCode = UnresolvedDirectMethodTrampolineFromCode; 1343ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee} 1353ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee 136caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstromvoid Thread::InitTid() { 137caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom tid_ = ::art::GetTid(); 138caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom} 139caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom 140caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstromvoid Thread::InitAfterFork() { 141caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom InitTid(); 1423147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes#if defined(__BIONIC__) 1433147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes // Work around a bionic bug. 1443147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes struct bionic_pthread_internal_t { 1453147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes void* next; 1463147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes void** pref; 1473147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes pthread_attr_t attr; 1483147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes pid_t kernel_id; 1493147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes // et cetera. we just need 'kernel_id' so we can stop here. 1503147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes }; 1513147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes reinterpret_cast<bionic_pthread_internal_t*>(pthread_self())->kernel_id = tid_; 1523147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes#endif 153caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom} 154caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom 15578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromvoid* Thread::CreateCallback(void* arg) { 15693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* self = reinterpret_cast<Thread*>(arg); 15793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Runtime* runtime = Runtime::Current(); 15893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 15993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes self->Attach(runtime); 16093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 16193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Wait until it's safe to start running code. (There may have been a suspend-all 16293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // in progress while we were starting up.) 16393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes runtime->GetThreadList()->WaitForGo(); 16493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 16547179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes { 16647179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_EQ(self->GetState(), Thread::kRunnable); 16700fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom SirtRef<String> thread_name(self->GetName()); 16800fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom SetThreadName(thread_name->ToModifiedUtf8().c_str()); 16947179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes } 17047179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes 171872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes Dbg::PostThreadStart(self); 17293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 17393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Invoke the 'run' method of our java.lang.Thread. 17493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes CHECK(self->peer_ != NULL); 17593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Object* receiver = self->peer_; 176038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes Method* m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(gThread_run); 17793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes m->Invoke(self, receiver, NULL, NULL); 17893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 17993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Detach. 18093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes runtime->GetThreadList()->Unregister(); 18193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 182b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro return NULL; 183b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 184b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 18593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughesvoid SetVmData(Object* managed_thread, Thread* native_thread) { 186038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes gThread_vmData->SetInt(managed_thread, reinterpret_cast<uintptr_t>(native_thread)); 18793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 18893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 18901158d7a57c8321370667a6045220237d16e0da8Elliott HughesThread* Thread::FromManagedThread(JNIEnv* env, jobject java_thread) { 19001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes Object* thread = Decode<Object*>(env, java_thread); 19101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes return reinterpret_cast<Thread*>(static_cast<uintptr_t>(gThread_vmData->GetInt(thread))); 19201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes} 19301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 1947502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughessize_t FixStackSize(size_t stack_size) { 1957502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // A stack size of zero means "use the default". 196d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (stack_size == 0) { 197d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes stack_size = Runtime::Current()->GetDefaultStackSize(); 198d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 19961e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 2007502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // It's not possible to request a stack smaller than the system-defined PTHREAD_STACK_MIN. 2017502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes if (stack_size < PTHREAD_STACK_MIN) { 2027502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = PTHREAD_STACK_MIN; 2037502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes } 2047502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2057502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // It's likely that callers are trying to ensure they have at least a certain amount of 2067502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // stack space, so we should add our reserved space on top of what they requested, rather 2077502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // than implicitly take it away from them. 2087502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size += Thread::kStackOverflowReservedBytes; 2097502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2107502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // Some systems require the stack size to be a multiple of the system page size, so round up. 2117502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = RoundUp(stack_size, kPageSize); 2127502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2137502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes return stack_size; 2147502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes} 2157502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2167502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughesvoid Thread::Create(Object* peer, size_t stack_size) { 2177502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes CHECK(peer != NULL); 2187502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2197502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = FixStackSize(stack_size); 2207502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 22193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* native_thread = new Thread; 22293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes native_thread->peer_ = peer; 22393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 22493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Thread.start is synchronized, so we know that vmData is 0, 22593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // and know that we're not racing to assign it. 22693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes SetVmData(peer, native_thread); 22761e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 22847179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes { 22947179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait); 23047179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes pthread_t new_pthread; 23147179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes pthread_attr_t attr; 23247179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread"); 23347179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), "PTHREAD_CREATE_DETACHED"); 23447179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size); 23547179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_create, (&new_pthread, &attr, Thread::CreateCallback, native_thread), "new thread"); 23647179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread"); 23747179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes } 23861e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 23993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Let the child know when it's safe to start running. 24093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Runtime::Current()->GetThreadList()->SignalGo(native_thread); 24193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 24261e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 24393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughesvoid Thread::Attach(const Runtime* runtime) { 24493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitCpu(); 24593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitFunctionPointers(); 24661e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 24793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes thin_lock_id_ = Runtime::Current()->GetThreadList()->AllocThreadId(); 248be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 249caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom InitTid(); 25093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitStackHwm(); 251dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 2523147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach"); 253a5780dad67556297c8ca5f2608c53b193e6c4514Elliott Hughes 25493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes jni_env_ = new JNIEnvExt(this, runtime->GetJavaVM()); 2555fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 2567a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes runtime->GetThreadList()->Register(); 25793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 25893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 25993e74e8d879270071c3aa163f8495ada8d21f42fElliott HughesThread* Thread::Attach(const Runtime* runtime, const char* name, bool as_daemon) { 26093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* self = new Thread; 26193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes self->Attach(runtime); 26293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 2637a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes self->SetState(Thread::kNative); 26493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 2655fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes // If we're the main thread, ClassLinker won't be created until after we're attached, 2665fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes // so that thread needs a two-stage attach. Regular threads don't need this hack. 2675fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes if (self->thin_lock_id_ != ThreadList::kMainId) { 2685fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes self->CreatePeer(name, as_daemon); 2695fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes } 2705fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 271726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes self->GetJniEnv()->locals.AssertEmpty(); 272726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes 2735fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes return self; 2745fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes} 2755fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 276d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughesjobject GetWellKnownThreadGroup(JNIEnv* env, const char* field_name) { 277726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jclass> thread_group_class(env, env->FindClass("java/lang/ThreadGroup")); 278726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes jfieldID fid = env->GetStaticFieldID(thread_group_class.get(), field_name, "Ljava/lang/ThreadGroup;"); 279726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes return env->GetStaticObjectField(thread_group_class.get(), fid); 280d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes} 281d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 2825fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughesvoid Thread::CreatePeer(const char* name, bool as_daemon) { 2835fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes JNIEnv* env = jni_env_; 2845fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 285d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes const char* field_name = (GetThinLockId() == ThreadList::kMainId) ? "mMain" : "mSystem"; 286726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jobject> thread_group(env, GetWellKnownThreadGroup(env, field_name)); 287726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF(name)); 2888daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes jint thread_priority = GetNativePriority(); 2895fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes jboolean thread_is_daemon = as_daemon; 2905fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 291726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Thread")); 292726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes jmethodID mid = env->GetMethodID(c.get(), "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V"); 293330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes 294726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jobject> peer(env, 295726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes env->NewObject(c.get(), mid, thread_group.get(), thread_name.get(), thread_priority, thread_is_daemon)); 296726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes peer_ = DecodeJObject(peer.get()); 2977a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes SetVmData(peer_, Thread::Current()); 298d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 29900fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom SirtRef<String> peer_thread_name(GetName()); 30000fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom if (peer_thread_name.get() == NULL) { 30100fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // The Thread constructor should have set the Thread.name to a 30200fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // non-null value. However, because we can run without code 30300fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // available (in the compiler, in tests), we manually assign the 30400fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // fields the constructor should have set. 30500fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom gThread_daemon->SetBoolean(peer_, thread_is_daemon); 30600fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom gThread_group->SetObject(peer_, Decode<Object*>(env, thread_group.get())); 30700fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom gThread_name->SetObject(peer_, Decode<Object*>(env, thread_name.get())); 30800fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom gThread_priority->SetInt(peer_, thread_priority); 30900fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom peer_thread_name.reset(GetName()); 31000fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom } 31100fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // thread_name may have been null, so don't trust this to be non-null 31200fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom if (peer_thread_name.get() != NULL) { 31300fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom SetThreadName(GetName()->ToModifiedUtf8().c_str()); 31400fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom } 315726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes 316726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes // Pre-allocate an OutOfMemoryError for the double-OOME case. 317726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ThrowNewException("Ljava/lang/OutOfMemoryError;", 318726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes "OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available"); 319726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jthrowable> exception(env, env->ExceptionOccurred()); 320726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes env->ExceptionClear(); 321726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes pre_allocated_OutOfMemoryError_ = Decode<Throwable*>(env, exception.get()); 32261e019d291583029c01b61b93bea750f2b663c37Carl Shapiro} 32361e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 324be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::InitStackHwm() { 325be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes pthread_attr_t attributes; 3263147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes CHECK_PTHREAD_CALL(pthread_getattr_np, (pthread_self(), &attributes), __FUNCTION__); 327be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 328932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers void* temp_stack_base; 329932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, &temp_stack_base, &stack_size_), 330932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers __FUNCTION__); 331932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers stack_base_ = reinterpret_cast<byte*>(temp_stack_base); 332be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 333932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers if (stack_size_ <= kStackOverflowReservedBytes) { 334932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers LOG(FATAL) << "attempt to attach a thread with a too-small stack (" << stack_size_ << " bytes)"; 335be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 336449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes 337932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // Set stack_end_ to the bottom of the stack saving space of stack overflows 338932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers ResetDefaultStackEnd(); 339449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes 340449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes // Sanity check. 341449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes int stack_variable; 342449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes CHECK_GT(&stack_variable, (void*) stack_end_); 343be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 3448d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); 345be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes} 346be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 347e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughesvoid Thread::Dump(std::ostream& os, bool dump_pending_exception) const { 348d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes DumpState(os); 349d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes DumpStack(os); 350e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes if (dump_pending_exception && IsExceptionPending()) { 351e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes os << "Pending " << PrettyTypeOf(GetException()) << " on thread:\n"; 352e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes os << GetException()->Dump(); 353e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes } 354d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes} 355d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 356d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesstd::string GetSchedulerGroup(pid_t tid) { 357d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // /proc/<pid>/group looks like this: 358d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // 2:devices:/ 359d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // 1:cpuacct,cpu:/ 360d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // We want the third field from the line whose second field contains the "cpu" token. 361d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string cgroup_file; 362d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (!ReadFileToString("/proc/self/cgroup", &cgroup_file)) { 363d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes return ""; 364d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 365d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::vector<std::string> cgroup_lines; 366d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes Split(cgroup_file, '\n', cgroup_lines); 367d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes for (size_t i = 0; i < cgroup_lines.size(); ++i) { 368d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::vector<std::string> cgroup_fields; 369d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes Split(cgroup_lines[i], ':', cgroup_fields); 370d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::vector<std::string> cgroups; 371d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes Split(cgroup_fields[1], ',', cgroups); 372d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes for (size_t i = 0; i < cgroups.size(); ++i) { 373d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (cgroups[i] == "cpu") { 374d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes return cgroup_fields[2].substr(1); // Skip the leading slash. 375d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 376d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 377d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 378d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes return ""; 379d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes} 380d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 381fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott HughesString* Thread::GetName() const { 382fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes return (peer_ != NULL) ? reinterpret_cast<String*>(gThread_name->GetObject(peer_)) : NULL; 383fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes} 384fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 385d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesvoid Thread::DumpState(std::ostream& os) const { 386d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::string thread_name("<native thread without managed peer>"); 387d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::string group_name; 388d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes int priority; 389d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes bool is_daemon = false; 390d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 391d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (peer_ != NULL) { 392038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes String* thread_name_string = reinterpret_cast<String*>(gThread_name->GetObject(peer_)); 393d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes thread_name = (thread_name_string != NULL) ? thread_name_string->ToModifiedUtf8() : "<null>"; 394038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes priority = gThread_priority->GetInt(peer_); 395038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes is_daemon = gThread_daemon->GetBoolean(peer_); 396d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 397038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes Object* thread_group = gThread_group->GetObject(peer_); 398d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (thread_group != NULL) { 399038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes String* group_name_string = reinterpret_cast<String*>(gThreadGroup_name->GetObject(thread_group)); 400d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes group_name = (group_name_string != NULL) ? group_name_string->ToModifiedUtf8() : "<null>"; 401d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 402d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } else { 403d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes // This name may be truncated, but it's the best we can do in the absence of a managed peer. 404dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes std::string stats; 405dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes if (ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { 406dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes size_t start = stats.find('(') + 1; 407dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes size_t end = stats.find(')') - start; 408dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes thread_name = stats.substr(start, end); 409dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes } 410d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes priority = GetNativePriority(); 411dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes } 412d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 413d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int policy; 414d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes sched_param sp; 4153147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes CHECK_PTHREAD_CALL(pthread_getschedparam, (pthread_self(), &policy, &sp), __FUNCTION__); 416d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 417d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string scheduler_group(GetSchedulerGroup(GetTid())); 418d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (scheduler_group.empty()) { 419d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_group = "default"; 420d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 421d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 422d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << '"' << thread_name << '"'; 423d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (is_daemon) { 424d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " daemon"; 425d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 426d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " prio=" << priority 427dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes << " tid=" << GetThinLockId() 42893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes << " " << GetState() << "\n"; 429d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 430d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | group=\"" << group_name << "\"" 4318d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes << " sCount=" << suspend_count_ 432234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes << " dsCount=" << debug_suspend_count_ 433dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes << " obj=" << reinterpret_cast<void*>(peer_) 434d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " self=" << reinterpret_cast<const void*>(this) << "\n"; 435d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | sysTid=" << GetTid() 436d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " nice=" << getpriority(PRIO_PROCESS, GetTid()) 437d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " sched=" << policy << "/" << sp.sched_priority 438d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " cgrp=" << scheduler_group 4393147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes << " handle=" << pthread_self() << "\n"; 440d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 441d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // Grab the scheduler stats for this thread. 442d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string scheduler_stats; 443d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", GetTid()).c_str(), &scheduler_stats)) { 444d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'. 445d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } else { 446d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_stats = "0 0 0"; 447d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 448d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 449d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int utime = 0; 450d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int stime = 0; 451d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int task_cpu = 0; 452bfe487be25652c5456236661b9d9c3579d2296c1Elliott Hughes GetTaskStats(GetTid(), utime, stime, task_cpu); 453d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 454d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | schedstat=( " << scheduler_stats << " )" 455d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " utm=" << utime 456d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " stm=" << stime 457d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " core=" << task_cpu 458d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " HZ=" << sysconf(_SC_CLK_TCK) << "\n"; 459a09576416788b916095739e43a16917e7948f3a4Elliott Hughes} 460a09576416788b916095739e43a16917e7948f3a4Elliott Hughes 461b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogersvoid Thread::PushNativeToManagedRecord(NativeToManagedRecord* record) { 462b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers Method **sp = top_of_managed_stack_.GetSP(); 463b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers#ifndef NDEBUG 464b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers if (sp != NULL) { 465b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers Method* m = *sp; 466b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers Heap::VerifyObject(m); 467b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers DCHECK((m == NULL) || m->IsMethod()); 468b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers } 469b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers#endif 470b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers record->last_top_of_managed_stack_ = reinterpret_cast<void*>(sp); 471b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers record->last_top_of_managed_stack_pc_ = top_of_managed_stack_pc_; 472b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers record->link_ = native_to_managed_record_; 473b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers native_to_managed_record_ = record; 474b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers top_of_managed_stack_.SetSP(NULL); 475b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers} 476b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers 477b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogersvoid Thread::PopNativeToManagedRecord(const NativeToManagedRecord& record) { 478b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers native_to_managed_record_ = record.link_; 479b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers top_of_managed_stack_.SetSP(reinterpret_cast<Method**>(record.last_top_of_managed_stack_)); 480b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers top_of_managed_stack_pc_ = record.last_top_of_managed_stack_pc_; 481b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers} 482b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers 483d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughesstruct StackDumpVisitor : public Thread::StackVisitor { 4848e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes StackDumpVisitor(std::ostream& os, const Thread* thread) 4858e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes : os(os), thread(thread), frame_count(0) { 486d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 487d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 488bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers virtual ~StackDumpVisitor() { 489d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 490d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 491bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers void VisitFrame(const Frame& frame, uintptr_t pc) { 4929086572fa809d1a19d886b467e4da3ce42016982Ian Rogers if (!frame.HasMethod()) { 4939086572fa809d1a19d886b467e4da3ce42016982Ian Rogers return; 4949086572fa809d1a19d886b467e4da3ce42016982Ian Rogers } 495d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 496d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Method* m = frame.GetMethod(); 497d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Class* c = m->GetDeclaringClass(); 4988e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 499d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes const DexFile& dex_file = class_linker->FindDexFile(c->GetDexCache()); 500d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 501d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes os << " at " << PrettyMethod(m, false); 502d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (m->IsNative()) { 503d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes os << "(Native method)"; 504d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } else { 505bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers int line_number = dex_file.GetLineNumFromPC(m, m->ToDexPC(pc)); 506f28bc5b6df6edb4b22aeca360ff102a95f3155e1Brian Carlstrom SirtRef<String> source_file(c->GetSourceFile()); 507f28bc5b6df6edb4b22aeca360ff102a95f3155e1Brian Carlstrom os << "(" << (source_file.get() != NULL ? source_file->ToModifiedUtf8() : "unavailable") 508f28bc5b6df6edb4b22aeca360ff102a95f3155e1Brian Carlstrom << ":" << line_number << ")"; 509d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 510d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes os << "\n"; 5118e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 5128e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (frame_count++ == 0) { 5138e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Monitor::DescribeWait(os, thread); 5148e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 515d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 516d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 517d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::ostream& os; 5188e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes const Thread* thread; 5198e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes int frame_count; 520d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes}; 521d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 522d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesvoid Thread::DumpStack(std::ostream& os) const { 5238e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes StackDumpVisitor dumper(os, this); 524d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes WalkStack(&dumper); 525e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes} 526e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes 5278d768a954b101a9532f980253ac46be2c53aba11Elliott HughesThread::State Thread::SetState(Thread::State new_state) { 5288d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes Thread::State old_state = state_; 5298d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (old_state == new_state) { 5308d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes return old_state; 5318d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 5328d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 5338d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes volatile void* raw = reinterpret_cast<volatile void*>(&state_); 5348d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes volatile int32_t* addr = reinterpret_cast<volatile int32_t*>(raw); 5358d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 5368d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (new_state == Thread::kRunnable) { 5378d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes /* 5388d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Change our status to Thread::kRunnable. The transition requires 5398d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * that we check for pending suspension, because the VM considers 5408d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * us to be "asleep" in all other states, and another thread could 5418d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * be performing a GC now. 5428d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5438d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * The order of operations is very significant here. One way to 5448d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * do this wrong is: 5458d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5468d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * GCing thread Our thread (in kNative) 5478d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * ------------ ---------------------- 5488d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * check suspend count (== 0) 5498d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * SuspendAllThreads() 5508d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * grab suspend-count lock 5518d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * increment all suspend counts 5528d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * release suspend-count lock 5538d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * check thread state (== kNative) 5548d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * all are suspended, begin GC 5558d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * set state to kRunnable 5568d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * (continue executing) 5578d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5588d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We can correct this by grabbing the suspend-count lock and 5598d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * performing both of our operations (check suspend count, set 5608d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * state) while holding it, now we need to grab a mutex on every 5618d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * transition to kRunnable. 5628d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5638d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * What we do instead is change the order of operations so that 5648d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * the transition to kRunnable happens first. If we then detect 5658d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * that the suspend count is nonzero, we switch to kSuspended. 5668d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5678d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Appropriate compiler and memory barriers are required to ensure 5688d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * that the operations are observed in the expected order. 5698d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5708d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * This does create a small window of opportunity where a GC in 5718d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * progress could observe what appears to be a running thread (if 5728d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * it happens to look between when we set to kRunnable and when we 5738d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * switch to kSuspended). At worst this only affects assertions 5748d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * and thread logging. (We could work around it with some sort 5758d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * of intermediate "pre-running" state that is generally treated 5768d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * as equivalent to running, but that doesn't seem worthwhile.) 5778d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5788d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We can also solve this by combining the "status" and "suspend 5798d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * count" fields into a single 32-bit value. This trades the 5808d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * store/load barrier on transition to kRunnable for an atomic RMW 5818d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * op on all transitions and all suspend count updates (also, all 5828d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * accesses to status or the thread count require bit-fiddling). 5838d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * It also eliminates the brief transition through kRunnable when 5848d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * the thread is supposed to be suspended. This is possibly faster 5858d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * on SMP and slightly more correct, but less convenient. 5868d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes */ 5878d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes android_atomic_acquire_store(new_state, addr); 5888d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (ANNOTATE_UNPROTECTED_READ(suspend_count_) != 0) { 5898d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes Runtime::Current()->GetThreadList()->FullSuspendCheck(this); 5908d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 5918d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } else { 5928d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes /* 5938d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Not changing to Thread::kRunnable. No additional work required. 5948d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5958d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We use a releasing store to ensure that, if we were runnable, 5968d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * any updates we previously made to objects on the managed heap 5978d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * will be observed before the state change. 5988d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes */ 5998d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes android_atomic_release_store(new_state, addr); 6008d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 6018d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 6028d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes return old_state; 6038d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes} 6048d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 6058d768a954b101a9532f980253ac46be2c53aba11Elliott Hughesvoid Thread::WaitUntilSuspended() { 6068d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes // TODO: dalvik dropped the waiting thread's priority after a while. 6078d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes // TODO: dalvik timed out and aborted. 6088d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes useconds_t delay = 0; 6098d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes while (GetState() == Thread::kRunnable) { 6108d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes useconds_t new_delay = delay * 2; 6118d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_GE(new_delay, delay); 6128d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes delay = new_delay; 6138d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (delay == 0) { 6148d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes sched_yield(); 6158d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes delay = 10000; 6168d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } else { 6178d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes usleep(delay); 6188d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 6198d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 6208d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes} 6218d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 622be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::ThreadExitCallback(void* arg) { 623be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes Thread* self = reinterpret_cast<Thread*>(arg); 624be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self; 625b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 626b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 627be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::Startup() { 628b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro // Allocate a TLS slot. 6298d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_key_create, (&Thread::pthread_key_self_, Thread::ThreadExitCallback), "self key"); 630b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 631b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro // Double-check the TLS slot allocation. 632b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro if (pthread_getspecific(pthread_key_self_) != NULL) { 633be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes LOG(FATAL) << "newly-created pthread TLS slot is not NULL"; 634b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro } 635038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes} 636038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 6378e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 6388e4aac52962d54cb4be2078b9cd95685e067133aElliott HughesClass* FindPrimitiveClassOrDie(ClassLinker* class_linker, char descriptor) { 6398e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* c = class_linker->FindPrimitiveClass(descriptor); 6408e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(c != NULL) << descriptor; 6418e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return c; 6428e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 6438e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6448e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 6458e4aac52962d54cb4be2078b9cd95685e067133aElliott HughesClass* FindClassOrDie(ClassLinker* class_linker, const char* descriptor) { 6468e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* c = class_linker->FindSystemClass(descriptor); 6478e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(c != NULL) << descriptor; 6488e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return c; 6498e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 6508e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6518e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 6526b4ef025af12b158d117fc80fc79acf620f411a0Brian CarlstromField* FindFieldOrDie(Class* c, const char* name, const char* descriptor) { 6536b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom Field* f = c->FindDeclaredInstanceField(name, descriptor); 6546b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom CHECK(f != NULL) << PrettyClass(c) << " " << name << " " << descriptor; 6558e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return f; 6568e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 6578e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6588e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 6598e4aac52962d54cb4be2078b9cd95685e067133aElliott HughesMethod* FindMethodOrDie(Class* c, const char* name, const char* signature) { 6608e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Method* m = c->FindVirtualMethod(name, signature); 6618e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(m != NULL) << PrettyClass(c) << " " << name << " " << signature; 6628e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return m; 6638e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 6648e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 665038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesvoid Thread::FinishStartup() { 666b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom Thread* self = Thread::Current(); 667b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom 668b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom // Need to be kRunnable for FindClass 669b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom ScopedThreadStateChange tsc(self, Thread::kRunnable); 670b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom 671038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes // Now the ClassLinker is ready, we can find the various Class*, Field*, and Method*s we need. 672038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 6738e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6748e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* Thread_class = FindClassOrDie(class_linker, "Ljava/lang/Thread;"); 6758e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* ThreadGroup_class = FindClassOrDie(class_linker, "Ljava/lang/ThreadGroup;"); 6768e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* UncaughtExceptionHandler_class = FindClassOrDie(class_linker, "Ljava/lang/Thread$UncaughtExceptionHandler;"); 6778e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThreadLock = FindClassOrDie(class_linker, "Ljava/lang/ThreadLock;"); 6788e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThrowable = FindClassOrDie(class_linker, "Ljava/lang/Throwable;"); 6798e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6806b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_daemon = FindFieldOrDie(Thread_class, "daemon", "Z"); 6816b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_group = FindFieldOrDie(Thread_class, "group", "Ljava/lang/ThreadGroup;"); 6826b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_lock = FindFieldOrDie(Thread_class, "lock", "Ljava/lang/ThreadLock;"); 6836b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_name = FindFieldOrDie(Thread_class, "name", "Ljava/lang/String;"); 6846b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_priority = FindFieldOrDie(Thread_class, "priority", "I"); 6856b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_uncaughtHandler = FindFieldOrDie(Thread_class, "uncaughtHandler", "Ljava/lang/Thread$UncaughtExceptionHandler;"); 6866b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_vmData = FindFieldOrDie(Thread_class, "vmData", "I"); 6876b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThreadGroup_name = FindFieldOrDie(ThreadGroup_class, "name", "Ljava/lang/String;"); 6886b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThreadLock_thread = FindFieldOrDie(gThreadLock, "thread", "Ljava/lang/Thread;"); 6898e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6908e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_run = FindMethodOrDie(Thread_class, "run", "()V"); 6918e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThreadGroup_removeThread = FindMethodOrDie(ThreadGroup_class, "removeThread", "(Ljava/lang/Thread;)V"); 6928e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gUncaughtExceptionHandler_uncaughtException = FindMethodOrDie(UncaughtExceptionHandler_class, 6938e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes "uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V"); 69401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 69501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Finish attaching the main thread. 696df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom self->CreatePeer("main", false); 697df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom 6986b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom const Field* Thread_contextClassLoader = FindFieldOrDie(Thread_class , "contextClassLoader", "Ljava/lang/ClassLoader;"); 699df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom Thread_contextClassLoader->SetObject(self->GetPeer(), self->GetClassLoaderOverride()); 700b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 701b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 702c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughesvoid Thread::Shutdown() { 7038d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key"); 704c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes} 705c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes 7068e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesuint32_t Thread::LockOwnerFromThreadLock(Object* thread_lock) { 7078e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (thread_lock == NULL || thread_lock->GetClass() != gThreadLock) { 7088e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 7098e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 7108e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Object* managed_thread = gThreadLock_thread->GetObject(thread_lock); 7118e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (managed_thread == NULL) { 7128e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 7138e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 7148e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes uintptr_t vmData = static_cast<uintptr_t>(gThread_vmData->GetInt(managed_thread)); 7158e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Thread* thread = reinterpret_cast<Thread*>(vmData); 7168e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (thread == NULL) { 7178e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 7188e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 7198e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return thread->GetThinLockId(); 7208e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 7218e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 722dcc247493fd8fb243e335c3ec08e5e625896a47cElliott HughesThread::Thread() 72347179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes : thin_lock_id_(0), 72447179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes tid_(0), 72547179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes peer_(NULL), 7268e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes top_of_managed_stack_(), 7278e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes top_of_managed_stack_pc_(0), 72885d1545e985ac689db4bad7849880e843707c862Elliott Hughes wait_mutex_(new Mutex("Thread wait mutex")), 72985d1545e985ac689db4bad7849880e843707c862Elliott Hughes wait_cond_(new ConditionVariable("Thread wait condition variable")), 7308daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes wait_monitor_(NULL), 7318daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes interrupted_(false), 732dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes wait_next_(NULL), 7338e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes monitor_enter_object_(NULL), 734dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes card_table_(0), 7358daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes stack_end_(NULL), 736dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes native_to_managed_record_(NULL), 737dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes top_sirt_(NULL), 738dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes jni_env_(NULL), 7398e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes state_(Thread::kNative), 740dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes self_(NULL), 741dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes runtime_(NULL), 742dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes exception_(NULL), 743dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes suspend_count_(0), 744234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes debug_suspend_count_(0), 74585d1545e985ac689db4bad7849880e843707c862Elliott Hughes class_loader_override_(NULL), 746418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes long_jump_context_(NULL), 747726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_(false), 748475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes pre_allocated_OutOfMemoryError_(NULL), 749475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes debug_invoke_req_(new DebugInvokeReq) { 750f5a7a476e7ea63e094ff0f011dccc170607e6f6bElliott Hughes CHECK_EQ((sizeof(Thread) % 4), 0U) << sizeof(Thread); 751dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes} 752dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 75302b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughesvoid MonitorExitVisitor(const Object* object, void*) { 75402b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes Object* entered_monitor = const_cast<Object*>(object); 7555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes entered_monitor->MonitorExit(Thread::Current()); 75602b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes} 75702b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 758c1674ed06662420213441ff2b818f2f71f9098dcElliott HughesThread::~Thread() { 75902b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited. 76093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes if (jni_env_ != NULL) { 76193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes jni_env_->monitors.VisitRoots(MonitorExitVisitor, NULL); 76293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes } 76302b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 76429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (peer_ != NULL) { 76529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 76629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // this.vmData = 0; 76793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes SetVmData(peer_, NULL); 76802b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 769872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes Dbg::PostThreadDeath(this); 77002b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 77129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // Thread.join() is implemented as an Object.wait() on the Thread.lock 77229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // object. Signal anyone who is waiting. 7735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* self = Thread::Current(); 774038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes Object* lock = gThread_lock->GetObject(peer_); 775038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes // (This conditional is only needed for tests, where Thread.lock won't have been set.) 7765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (lock != NULL) { 7775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->MonitorEnter(self); 7785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->NotifyAll(); 7795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->MonitorExit(self); 7805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 78202b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 783c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes delete jni_env_; 78402b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes jni_env_ = NULL; 78502b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 78602b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes SetState(Thread::kTerminated); 78785d1545e985ac689db4bad7849880e843707c862Elliott Hughes 78885d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete wait_cond_; 78985d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete wait_mutex_; 79085d1545e985ac689db4bad7849880e843707c862Elliott Hughes 79185d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete long_jump_context_; 792475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes 793475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes delete debug_invoke_req_; 794c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes} 795c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes 796accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughesvoid Thread::HandleUncaughtExceptions() { 797accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes if (!IsExceptionPending()) { 798accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes return; 799accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes } 800accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 801accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Get and clear the exception. 802accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Object* exception = GetException(); 803accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes ClearException(); 804accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 805accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // If the thread has its own handler, use that. 806accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Object* handler = gThread_uncaughtHandler->GetObject(peer_); 807accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes if (handler == NULL) { 808accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Otherwise use the thread group's default handler. 809accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes handler = gThread_group->GetObject(peer_); 810accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes } 811accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 812accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Call the handler. 813accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Method* m = handler->GetClass()->FindVirtualMethodForVirtualOrInterface(gUncaughtExceptionHandler_uncaughtException); 814accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Object* args[2]; 815accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes args[0] = peer_; 816accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes args[1] = exception; 817accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes m->Invoke(this, handler, reinterpret_cast<byte*>(&args), NULL); 818accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 819accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // If the handler threw, clear that exception too. 820accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes ClearException(); 821accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 8224514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom 8234514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstromvoid Thread::RemoveFromThreadGroup() { 8244514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom // this.group.removeThread(this); 8254514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom // group can be null if we're in the compiler or a test. 8264514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom Object* group = gThread_group->GetObject(peer_); 8274514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom if (group != NULL) { 8284514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom Method* m = group->GetClass()->FindVirtualMethodForVirtualOrInterface(gThreadGroup_removeThread); 8294514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom Object* args = peer_; 8304514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom m->Invoke(this, group, reinterpret_cast<byte*>(&args), NULL); 8314514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom } 8324514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom} 833accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 834408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogerssize_t Thread::NumSirtReferences() { 835a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers size_t count = 0; 83640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { 837a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers count += cur->NumberOfReferences(); 838a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 839a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return count; 840a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers} 841a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers 842408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogersbool Thread::SirtContains(jobject obj) { 843408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers Object** sirt_entry = reinterpret_cast<Object**>(obj); 84440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { 84540381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom if (cur->Contains(sirt_entry)) { 846a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return true; 847a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 848a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 849a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return false; 850a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers} 851a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers 8528dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liaovoid Thread::SirtVisitRoots(Heap::RootVisitor* visitor, void* arg) { 85340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { 8548dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao size_t num_refs = cur->NumberOfReferences(); 8558dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao for (size_t j = 0; j < num_refs; j++) { 85640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom Object* object = cur->GetReference(j); 8575e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom if (object != NULL) { 8585e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom visitor(object, arg); 8595e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom } 8608dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao } 8618dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao } 8628dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao} 8638dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 864408f79aeb676251ba35667a64e86c20638d7cb0bIan RogersObject* Thread::DecodeJObject(jobject obj) { 8650cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers DCHECK(CanAccessDirectReferences()); 866408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (obj == NULL) { 867408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return NULL; 868408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 869408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectRef ref = reinterpret_cast<IndirectRef>(obj); 870408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectRefKind kind = GetIndirectRefKind(ref); 871408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers Object* result; 872408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers switch (kind) { 873408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kLocal: 874408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 87569f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughes IndirectReferenceTable& locals = jni_env_->locals; 876cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(locals.Get(ref)); 877408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 878408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 879408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kGlobal: 880408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 881408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers JavaVMExt* vm = Runtime::Current()->GetJavaVM(); 882408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectReferenceTable& globals = vm->globals; 883408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers MutexLock mu(vm->globals_lock); 884cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(globals.Get(ref)); 885408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 886408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 887408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kWeakGlobal: 888408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 889408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers JavaVMExt* vm = Runtime::Current()->GetJavaVM(); 890408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectReferenceTable& weak_globals = vm->weak_globals; 891408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers MutexLock mu(vm->weak_globals_lock); 892cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(weak_globals.Get(ref)); 893408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (result == kClearedJniWeakGlobal) { 894408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // This is a special case where it's okay to return NULL. 895408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return NULL; 896408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 897408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 898408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 899408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kSirtOrInvalid: 900408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers default: 901408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // TODO: make stack indirect reference table lookup more efficient 902408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Check if this is a local reference in the SIRT 903408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (SirtContains(obj)) { 9040cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers result = *reinterpret_cast<Object**>(obj); // Read from SIRT 905c5bfa8f49d8548d7c685a99b411311ef56bedffaElliott Hughes } else if (jni_env_->work_around_app_jni_bugs) { 906408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Assume an invalid local reference is actually a direct pointer. 907408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers result = reinterpret_cast<Object*>(obj); 908408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } else { 909a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes result = kInvalidIndirectRefObject; 910408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 911408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 912408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 913408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (result == NULL) { 914a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes LOG(ERROR) << "JNI ERROR (app bug): use of deleted " << kind << ": " << obj; 915a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes JniAbort(NULL); 916a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes } else { 917a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes if (result != kInvalidIndirectRefObject) { 918a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes Heap::VerifyObject(result); 919a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes } 920408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 921408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return result; 922408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers} 923408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 9249b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liaoclass CountStackDepthVisitor : public Thread::StackVisitor { 9259b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 92629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes CountStackDepthVisitor() : depth_(0), skip_depth_(0), skipping_(true) {} 927d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 92829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes virtual void VisitFrame(const Frame& frame, uintptr_t pc) { 92929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // We want to skip frames up to and including the exception's constructor. 9309086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // Note we also skip the frame if it doesn't have a method (namely the callee 9319086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // save frame) 93225c3325bf95036bf325fc7cb21b4fd6d40282857Brian Carlstrom DCHECK(gThrowable != NULL); 9339086572fa809d1a19d886b467e4da3ce42016982Ian Rogers if (skipping_ && frame.HasMethod() && !gThrowable->IsAssignableFrom(frame.GetMethod()->GetDeclaringClass())) { 93429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes skipping_ = false; 93529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 93629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (!skipping_) { 93729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes ++depth_; 93829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } else { 93929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes ++skip_depth_; 94029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 9419b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9429b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 9439b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao int GetDepth() const { 944aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return depth_; 9459b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9469b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 94729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int GetSkipDepth() const { 94829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes return skip_depth_; 94929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 95029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 9519b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao private: 952aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t depth_; 95329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes uint32_t skip_depth_; 95429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes bool skipping_; 9559b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao}; 9569b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 957aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogersclass BuildInternalStackTraceVisitor : public Thread::StackVisitor { 9589b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 95929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes explicit BuildInternalStackTraceVisitor(int depth, int skip_depth, ScopedJniThreadState& ts) 960726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes : skip_depth_(skip_depth), count_(0), pc_trace_(NULL), method_trace_(NULL), local_ref_(NULL) { 961aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Allocate method trace with an extra slot that will hold the PC trace 96201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes method_trace_ = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(depth + 1); 963726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (method_trace_ == NULL) { 964726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes return; 965726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 966aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Register a local reference as IntArray::Alloc may trigger GC 967aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers local_ref_ = AddLocalReference<jobject>(ts.Env(), method_trace_); 968aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers pc_trace_ = IntArray::Alloc(depth); 969726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (pc_trace_ == NULL) { 970726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes return; 971726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 972aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#ifdef MOVING_GARBAGE_COLLECTOR 973aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Re-read after potential GC 974aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace = Decode<ObjectArray<Object>*>(ts.Env(), local_ref_); 975aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#endif 976aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Save PC trace in last element of method trace, also places it into the 977aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // object graph. 978aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace_->Set(depth, pc_trace_); 9799b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9809b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 981aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers virtual ~BuildInternalStackTraceVisitor() {} 9829b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 983bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers virtual void VisitFrame(const Frame& frame, uintptr_t pc) { 984726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (method_trace_ == NULL || pc_trace_ == NULL) { 985726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes return; // We're probably trying to fillInStackTrace for an OutOfMemoryError. 986726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 98729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (skip_depth_ > 0) { 98829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes skip_depth_--; 98929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes return; 99029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 991aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace_->Set(count_, frame.GetMethod()); 992bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers pc_trace_->Set(count_, pc); 993aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ++count_; 9949b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9959b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 996aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers jobject GetInternalStackTrace() const { 997aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return local_ref_; 9989b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9999b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 10009b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao private: 100129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // How many more frames to skip. 100229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int32_t skip_depth_; 1003aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Current position down stack trace 1004aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t count_; 1005aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Array of return PC values 1006aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers IntArray* pc_trace_; 1007aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // An array of the methods on the stack, the last entry is a reference to the 1008aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // PC trace 1009aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ObjectArray<Object>* method_trace_; 1010aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Local indirect reference table entry for method trace 1011aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers jobject local_ref_; 10129b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao}; 10139b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1014a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes// TODO: remove this. 1015a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughesuintptr_t ManglePc(uintptr_t pc) { 1016a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // Move the PC back 2 bytes as a call will frequently terminate the 1017a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // decoding of a particular instruction and we want to make sure we 1018a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // get the Dex PC of the instruction with the call and not the 1019a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // instruction following. 1020a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes if (pc > 0) { pc -= 2; } 1021a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes return pc; 1022a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes} 1023a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes 1024a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes// TODO: remove this. 1025a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughesuintptr_t DemanglePc(uintptr_t pc) { 1026a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // Revert mangling for the case where we need the PC to return to the upcall 1027a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes return pc + 2; 1028a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes} 1029f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers 103040381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstromvoid Thread::PushSirt(StackIndirectReferenceTable* sirt) { 103140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom sirt->SetLink(top_sirt_); 103240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom top_sirt_ = sirt; 103340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom} 103440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom 103540381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian CarlstromStackIndirectReferenceTable* Thread::PopSirt() { 103640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom CHECK(top_sirt_ != NULL); 103740381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom StackIndirectReferenceTable* sirt = top_sirt_; 103840381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom top_sirt_ = top_sirt_->GetLink(); 103940381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom return sirt; 104040381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom} 104140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom 1042aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogersvoid Thread::WalkStack(StackVisitor* visitor) const { 1043d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Frame frame = GetTopOfStack(); 1044a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes uintptr_t pc = ManglePc(top_of_managed_stack_pc_); 10459b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao // TODO: enable this CHECK after native_to_managed_record_ is initialized during startup. 10469b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao // CHECK(native_to_managed_record_ != NULL); 10479b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao NativeToManagedRecord* record = native_to_managed_record_; 10484417536522fd2a9d8215d8672331984769c9520bShih-wei Liao 1049bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers while (frame.GetSP() != 0) { 10509b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao for ( ; frame.GetMethod() != 0; frame.Next()) { 10513320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom // DCHECK(frame.GetMethod()->IsWithinCode(pc)); // TODO: restore IsWithinCode 10523ddac99d4dc6a036fac59d8f0bdc664ef619fb04Shih-wei Liao visitor->VisitFrame(frame, pc); 1053a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes pc = ManglePc(frame.GetReturnPC()); 10549b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 10559b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao if (record == NULL) { 10569b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao break; 10579b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 1058bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // last_tos should return Frame instead of sp? 1059ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers frame.SetSP(reinterpret_cast<Method**>(record->last_top_of_managed_stack_)); 1060a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes pc = ManglePc(record->last_top_of_managed_stack_pc_); 1061bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers record = record->link_; 1062bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1063bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers} 1064bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 106567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogersvoid Thread::WalkStackUntilUpCall(StackVisitor* visitor, bool include_upcall) const { 1066bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Frame frame = GetTopOfStack(); 1067a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes uintptr_t pc = ManglePc(top_of_managed_stack_pc_); 1068bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1069bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (frame.GetSP() != 0) { 1070bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers for ( ; frame.GetMethod() != 0; frame.Next()) { 10713320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom // DCHECK(frame.GetMethod()->IsWithinCode(pc)); // TODO: restore IsWithinCode 10723ddac99d4dc6a036fac59d8f0bdc664ef619fb04Shih-wei Liao visitor->VisitFrame(frame, pc); 1073a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes pc = ManglePc(frame.GetReturnPC()); 1074bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 107567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers if (include_upcall) { 107667375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers visitor->VisitFrame(frame, pc); 107767375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers } 107855df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao } 107955df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao} 108055df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 108101158d7a57c8321370667a6045220237d16e0da8Elliott Hughesjobject Thread::CreateInternalStackTrace(JNIEnv* env) const { 1082aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Compute depth of stack 10839b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao CountStackDepthVisitor count_visitor; 10849b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao WalkStack(&count_visitor); 10859b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao int32_t depth = count_visitor.GetDepth(); 108629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int32_t skip_depth = count_visitor.GetSkipDepth(); 10874417536522fd2a9d8215d8672331984769c9520bShih-wei Liao 1088aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Transition into runnable state to work on Object*/Array* 108901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes ScopedJniThreadState ts(env); 1090aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 1091aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Build internal stack trace 109229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes BuildInternalStackTraceVisitor build_trace_visitor(depth, skip_depth, ts); 10939b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao WalkStack(&build_trace_visitor); 10944417536522fd2a9d8215d8672331984769c9520bShih-wei Liao 1095aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return build_trace_visitor.GetInternalStackTrace(); 1096aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers} 1097aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 109801158d7a57c8321370667a6045220237d16e0da8Elliott HughesjobjectArray Thread::InternalStackTraceToStackTraceElementArray(JNIEnv* env, jobject internal, 109901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobjectArray output_array, int* stack_depth) { 1100aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Transition into runnable state to work on Object*/Array* 1101aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ScopedJniThreadState ts(env); 1102aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Decode the internal stack trace into the depth, method trace and PC trace 1103aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ObjectArray<Object>* method_trace = 1104aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); 11059074b99bfbd5419e340f138a018b673ce71f77e8Ian Rogers int32_t depth = method_trace->GetLength() - 1; 1106aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); 1107aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 1108aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1109aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 111001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobjectArray result; 111101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes ObjectArray<StackTraceElement>* java_traces; 111201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes if (output_array != NULL) { 111301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Reuse the array we were given. 111401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes result = output_array; 111501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes java_traces = reinterpret_cast<ObjectArray<StackTraceElement>*>(Decode<Array*>(env, 111601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes output_array)); 111701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // ...adjusting the number of frames we'll write to not exceed the array length. 111801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes depth = std::min(depth, java_traces->GetLength()); 111901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } else { 112001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Create java_trace array and place in local reference table 112101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes java_traces = class_linker->AllocStackTraceElementArray(depth); 112230646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (java_traces == NULL) { 112330646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return NULL; 112430646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 112501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes result = AddLocalReference<jobjectArray>(ts.Env(), java_traces); 112601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } 112701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 112801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes if (stack_depth != NULL) { 112901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes *stack_depth = depth; 113001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } 113155df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 11329b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao for (int32_t i = 0; i < depth; ++i) { 1133aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Prepare parameters for StackTraceElement(String cls, String method, String file, int line) 1134aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers Method* method = down_cast<Method*>(method_trace->Get(i)); 1135aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t native_pc = pc_trace->Get(i); 1136aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers Class* klass = method->GetDeclaringClass(); 113738933579b15c74baa09b8713ee8bf715529d05ebElliott Hughes std::string class_name(PrettyDescriptor(klass->GetDescriptor())); 1138466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers int32_t line_number = -1; 1139466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers DexCache* dex_cache = klass->GetDexCache(); 1140466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers if (dex_cache != NULL) { 1141466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers const DexFile& dex_file = class_linker->FindDexFile(dex_cache); 1142466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers line_number = dex_file.GetLineNumFromPC(method, method->ToDexPC(native_pc)); 1143466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers } 1144aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Allocate element, potentially triggering GC 114540381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom // TODO: reuse class_name_object via Class::name_? 114640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom SirtRef<String> class_name_object(String::AllocFromModifiedUtf8(class_name.c_str())); 114740381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom if (class_name_object.get() == NULL) { 114840381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom return NULL; 114940381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom } 115040381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom StackTraceElement* obj = StackTraceElement::Alloc(class_name_object.get(), 115140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom method->GetName(), 115240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom klass->GetSourceFile(), 115340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom line_number); 115430646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (obj == NULL) { 115530646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return NULL; 115630646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1157aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#ifdef MOVING_GARBAGE_COLLECTOR 1158aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Re-read after potential GC 1159aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers java_traces = Decode<ObjectArray<Object>*>(ts.Env(), result); 1160aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace = down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); 1161aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); 1162aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#endif 116355df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao java_traces->Set(i, obj); 116455df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao } 1165aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return result; 116655df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao} 116755df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 1168fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughesvoid Thread::GetCurrentLocation(const char*& source_file, uint32_t& line_number) const { 1169fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes Frame f = top_of_managed_stack_; 1170fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes Method* m = f.GetMethod(); 1171fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes // TODO: can this ever happen? 1172fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (m->IsCalleeSaveMethod()) { 1173fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes f.Next(); 1174fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes m = f.GetMethod(); 1175fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 1176fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 1177fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1178fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes Class* c = m->GetDeclaringClass(); 1179fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes DexCache* dex_cache = c->GetDexCache(); 1180fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes const DexFile& dex_file = class_linker->FindDexFile(dex_cache); 1181fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes const DexFile::ClassDef* class_def = dex_file.FindClassDef(c->GetDescriptor()->ToModifiedUtf8()); 1182fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 1183fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes source_file = dex_file.dexGetSourceFile(*class_def); 1184fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 1185fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes uint32_t pc = ManglePc(f.GetReturnPC()); 1186fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes line_number = dex_file.GetLineNumFromPC(m, m->ToDexPC(pc)); 1187fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes} 1188fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 11895cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughesvoid Thread::ThrowNewExceptionF(const char* exception_class_descriptor, const char* fmt, ...) { 1190a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_list args; 1191a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_start(args, fmt); 11924a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes ThrowNewExceptionV(exception_class_descriptor, fmt, args); 1193a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_end(args); 11944a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes} 11954a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes 11964a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughesvoid Thread::ThrowNewExceptionV(const char* exception_class_descriptor, const char* fmt, va_list ap) { 11974a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes std::string msg; 11984a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes StringAppendV(&msg, fmt, ap); 11995cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes ThrowNewException(exception_class_descriptor, msg.c_str()); 12005cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes} 120137f7a40f6789bb287f287a9af00777af9d6428eeElliott Hughes 12025cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughesvoid Thread::ThrowNewException(const char* exception_class_descriptor, const char* msg) { 1203e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes // Convert "Ljava/lang/Exception;" into JNI-style "java/lang/Exception". 12040cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers CHECK_EQ('L', exception_class_descriptor[0]); 1205e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes std::string descriptor(exception_class_descriptor + 1); 12060cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers CHECK_EQ(';', descriptor[descriptor.length() - 1]); 1207e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes descriptor.erase(descriptor.length() - 1); 1208e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes 1209e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes JNIEnv* env = GetJniEnv(); 1210726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jclass> exception_class(env, env->FindClass(descriptor.c_str())); 121130646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (exception_class.get() == NULL) { 121230646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI FindClass failed: " 121330646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes << PrettyTypeOf(GetException()); 121430646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes CHECK(IsExceptionPending()); 121530646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return; 121630646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1217726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes int rc = env->ThrowNew(exception_class.get(), msg); 121830646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (rc != JNI_OK) { 121930646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI ThrowNew failed: " 122030646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes << PrettyTypeOf(GetException()); 122130646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes CHECK(IsExceptionPending()); 122230646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return; 122330646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1224a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes} 1225a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes 1226418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughesvoid Thread::ThrowOutOfMemoryError(Class* c, size_t byte_count) { 12272ced6a534157d5d963693346904389c19775d2daElliott Hughes std::string msg(StringPrintf("Failed to allocate a %zd-byte %s", byte_count, 12282ced6a534157d5d963693346904389c19775d2daElliott Hughes PrettyDescriptor(c->GetDescriptor()).c_str())); 12292ced6a534157d5d963693346904389c19775d2daElliott Hughes ThrowOutOfMemoryError(msg.c_str()); 12302ced6a534157d5d963693346904389c19775d2daElliott Hughes} 12312ced6a534157d5d963693346904389c19775d2daElliott Hughes 12322ced6a534157d5d963693346904389c19775d2daElliott Hughesvoid Thread::ThrowOutOfMemoryError(const char* msg) { 12332ced6a534157d5d963693346904389c19775d2daElliott Hughes LOG(ERROR) << StringPrintf("Throwing OutOfMemoryError \"%s\"%s", 12342ced6a534157d5d963693346904389c19775d2daElliott Hughes msg, (throwing_OutOfMemoryError_ ? " (recursive case)" : "")); 1235726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (!throwing_OutOfMemoryError_) { 1236726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_ = true; 1237accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes ThrowNewException("Ljava/lang/OutOfMemoryError;", NULL); 1238418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes } else { 1239726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes SetException(pre_allocated_OutOfMemoryError_); 1240418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes } 1241726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_ = false; 124279082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes} 124379082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes 1244accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1245498508c1187dc07d3eae5476784cde20f5224d93Elliott HughesThread* Thread::CurrentFromGdb() { 1246accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes return Thread::Current(); 1247accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 1248accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1249accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughesvoid Thread::DumpFromGdb() const { 12506b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom std::ostringstream ss; 12516b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom Dump(ss); 12526b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom std::string str = ss.str(); 12536b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom // log to stderr for debugging command line processes 12546b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom std::cerr << str; 12556b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom#ifdef HAVE_ANDROID_OS 12566b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom // log to logcat for debugging frameworks processes 12576b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom LOG(INFO) << str; 12586b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom#endif 1259accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 1260accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1261bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogersclass CatchBlockStackVisitor : public Thread::StackVisitor { 1262bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers public: 1263bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers CatchBlockStackVisitor(Class* to_find, Context* ljc) 126467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers : found_(false), to_find_(to_find), long_jump_context_(ljc), native_method_count_(0) { 126567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers#ifndef NDEBUG 126667375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_pc_ = 0xEBADC0DE; 126767375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_frame_.SetSP(reinterpret_cast<Method**>(0xEBADF00D)); 126867375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers#endif 126967375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers } 1270bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1271bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers virtual void VisitFrame(const Frame& fr, uintptr_t pc) { 1272bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (!found_) { 1273bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Method* method = fr.GetMethod(); 127467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers if (method == NULL) { 127567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // This is the upcall, we remember the frame and last_pc so that we may 127667375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // long jump to them 1277a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes handler_pc_ = DemanglePc(pc); 127867375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_frame_ = fr; 127967375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers return; 128067375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers } 128167375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uint32_t dex_pc = DexFile::kDexNoIndex; 12823320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom if (method->IsCalleeSaveMethod()) { 12839086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // ignore callee save method 12849086572fa809d1a19d886b467e4da3ce42016982Ian Rogers } else if (method->IsNative()) { 12859086572fa809d1a19d886b467e4da3ce42016982Ian Rogers native_method_count_++; 12869086572fa809d1a19d886b467e4da3ce42016982Ian Rogers } else { 12879086572fa809d1a19d886b467e4da3ce42016982Ian Rogers dex_pc = method->ToDexPC(pc); 1288bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1289bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (dex_pc != DexFile::kDexNoIndex) { 1290bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers uint32_t found_dex_pc = method->FindCatchBlock(to_find_, dex_pc); 1291bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (found_dex_pc != DexFile::kDexNoIndex) { 1292bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers found_ = true; 129367375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_pc_ = method->ToNativePC(found_dex_pc); 129467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_frame_ = fr; 1295bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1296bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1297bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (!found_) { 1298bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Caller may be handler, fill in callee saves in context 1299bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers long_jump_context_->FillCalleeSaves(fr); 1300bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 13011a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 13021a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 13031a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 1304bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Did we find a catch block yet? 1305bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers bool found_; 1306bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // The type of the exception catch block to find 1307bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Class* to_find_; 1308bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Frame with found handler or last frame if no handler found 1309bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Frame handler_frame_; 131067375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // PC to branch to for the handler 131167375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uintptr_t handler_pc_; 1312bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Context that will be the target of the long jump 1313bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Context* long_jump_context_; 131467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // Number of native methods passed in crawl (equates to number of SIRTs to pop) 131567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uint32_t native_method_count_; 1316bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers}; 1317bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1318ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogersvoid Thread::DeliverException() { 1319ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers Throwable *exception = GetException(); // Set exception on thread 1320ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers CHECK(exception != NULL); 1321bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1322bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Context* long_jump_context = GetLongJumpContext(); 1323bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers CatchBlockStackVisitor catch_finder(exception->GetClass(), long_jump_context); 132467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers WalkStackUntilUpCall(&catch_finder, true); 1325bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 132667375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers long_jump_context->SetSP(reinterpret_cast<intptr_t>(catch_finder.handler_frame_.GetSP())); 132767375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers long_jump_context->SetPC(catch_finder.handler_pc_); 1328bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers long_jump_context->DoLongJump(); 1329bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers} 1330bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1331bdb0391258abc54bf77c676e36847d28a783bfe5Ian RogersContext* Thread::GetLongJumpContext() { 133285d1545e985ac689db4bad7849880e843707c862Elliott Hughes Context* result = long_jump_context_; 1333bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (result == NULL) { 1334bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers result = Context::Create(); 133585d1545e985ac689db4bad7849880e843707c862Elliott Hughes long_jump_context_ = result; 13361a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 1337bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers return result; 13381a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao} 13391a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 13409fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughesconst Method* Thread::GetCurrentMethod() const { 13419fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes Method* m = top_of_managed_stack_.GetMethod(); 13429fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // We use JNI internally for exception throwing, so it's possible to arrive 13439fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // here via a "FromCode" function, in which case there's a synthetic 13449fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // callee-save method at the top of the stack. These shouldn't be user-visible, 13459fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // so if we find one, skip it and return the compiled method underneath. 13469fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes if (m->IsCalleeSaveMethod()) { 13479fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes Frame f = top_of_managed_stack_; 13489fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes f.Next(); 13499fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes m = f.GetMethod(); 13509fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes } 13519fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes return m; 13529fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes} 13539fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes 13545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesbool Thread::HoldsLock(Object* object) { 13555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (object == NULL) { 13565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 13575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 135824a3c2e9924e8765c4a9b4d383cb8f3b922f9c9fBrian Carlstrom return object->GetThinLockId() == thin_lock_id_; 13595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 13605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1361038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesbool Thread::IsDaemon() { 1362038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes return gThread_daemon->GetBoolean(peer_); 1363038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes} 1364038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 1365d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogersclass ReferenceMapVisitor : public Thread::StackVisitor { 1366d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers public: 1367d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers ReferenceMapVisitor(Context* context, Heap::RootVisitor* root_visitor, void* arg) : 1368d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers context_(context), root_visitor_(root_visitor), arg_(arg) { 1369d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1370d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1371d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers void VisitFrame(const Frame& frame, uintptr_t pc) { 1372d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Method* m = frame.GetMethod(); 13736a4be3a6226cec645cf905dd352e44f7968a7fa4Brian Carlstrom if (false) { 13746a4be3a6226cec645cf905dd352e44f7968a7fa4Brian Carlstrom LOG(INFO) << "Visiting stack roots in " << PrettyMethod(m, false) 13750dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom << StringPrintf("@ PC:%04x", m->ToDexPC(pc)); 13760dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom 13776a4be3a6226cec645cf905dd352e44f7968a7fa4Brian Carlstrom } 1378d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Process register map (which native and callee save methods don't have) 13793320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom if (!m->IsNative() && !m->IsCalleeSaveMethod()) { 1380d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers verifier::PcToReferenceMap map(m); 1381d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers const uint8_t* reg_bitmap = map.FindBitMap(m->ToDexPC(pc)); 1382d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers CHECK(reg_bitmap != NULL); 13833320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom const uint16_t* vmap = m->GetVmapTable(); 13840dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom // For all dex registers in the bitmap 13850dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom size_t num_regs = std::min(map.RegWidth() * 8, static_cast<size_t>(m->NumRegisters())); 13860dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom for (size_t reg = 0; reg < num_regs; ++reg) { 1387d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Does this register hold a reference? 1388d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers if (TestBitmap(reg, reg_bitmap)) { 1389d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Is the reference in the context or on the stack? 1390d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers bool in_context = false; 1391f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers uint32_t vmap_offset = 0xEBAD0FF5; 1392d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // TODO: take advantage of the registers being ordered 13933320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom for (int i = 0; i < m->GetVmapTableLength(); i++) { 13940dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom // stop if we find what we are are looking for or the INVALID_VREG that marks lr 13950dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom if (vmap[i] == 0xffff) { 13960dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom break; 13970dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom } 13983320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom if (vmap[i] == reg) { 1399d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers in_context = true; 1400d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers vmap_offset = i; 1401d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers break; 1402d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1403d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1404d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Object* ref; 1405d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers if (in_context) { 1406d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Compute the register we need to load from the context 1407d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers uint32_t spill_mask = m->GetCoreSpillMask(); 14080dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom CHECK_LT(vmap_offset, static_cast<uint32_t>(__builtin_popcount(spill_mask))); 1409f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers uint32_t matches = 0; 1410f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers uint32_t spill_shifts = 0; 1411f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers while (matches != (vmap_offset + 1)) { 14120dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom DCHECK_NE(spill_mask, 0u); 1413f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers matches += spill_mask & 1; // Add 1 if the low bit is set 1414f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_mask >>= 1; 1415f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_shifts++; 1416d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1417f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_shifts--; // wind back one as we want the last match 1418f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers ref = reinterpret_cast<Object*>(context_->GetGPR(spill_shifts)); 1419d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } else { 1420362f9bc807169bcfc8761dde067bbfb79b5ad0fdElliott Hughes ref = reinterpret_cast<Object*>(frame.GetVReg(m, reg)); 1421d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 14224f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao if (ref != NULL) { 14234f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao root_visitor_(ref, arg_); 14244f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao } 1425d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1426d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1427d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1428d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers context_->FillCalleeSaves(frame); 1429d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1430d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1431d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers private: 1432d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers bool TestBitmap(int reg, const uint8_t* reg_vector) { 1433d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers return ((reg_vector[reg / 8] >> (reg % 8)) & 0x01) != 0; 1434d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1435d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1436d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Context used to build up picture of callee saves 1437d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Context* context_; 1438d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Call-back when we visit a root 1439d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Heap::RootVisitor* root_visitor_; 1440d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Argument to call-back 1441d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers void* arg_; 1442d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers}; 1443d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1444d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogersvoid Thread::VisitRoots(Heap::RootVisitor* visitor, void* arg) { 1445d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (exception_ != NULL) { 1446d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes visitor(exception_, arg); 1447d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 1448d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (peer_ != NULL) { 1449d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes visitor(peer_, arg); 1450d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 1451726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (pre_allocated_OutOfMemoryError_ != NULL) { 1452726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes visitor(pre_allocated_OutOfMemoryError_, arg); 1453726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 145440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom if (class_loader_override_ != NULL) { 145540381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom visitor(class_loader_override_, arg); 145640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom } 1457410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes jni_env_->locals.VisitRoots(visitor, arg); 1458410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes jni_env_->monitors.VisitRoots(visitor, arg); 14598dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 14608dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao SirtVisitRoots(visitor, arg); 14618dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 1462d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Cheat and steal the long jump context. Assume that we are not doing a GC during exception 1463d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // delivery. 1464d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Context* context = GetLongJumpContext(); 1465d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Visit roots on this thread's stack 1466d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers ReferenceMapVisitor mapper(context, visitor, arg); 1467d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers WalkStack(&mapper); 1468410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes} 1469410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes 1470b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogersstatic const char* kStateNames[] = { 147193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "Terminated", 1472b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers "Runnable", 147393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "TimedWaiting", 1474b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers "Blocked", 1475b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers "Waiting", 147693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "Initializing", 147793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "Starting", 1478b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers "Native", 147993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "VmWait", 148093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "Suspended", 1481b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers}; 1482b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogersstd::ostream& operator<<(std::ostream& os, const Thread::State& state) { 14838e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes int32_t int_state = static_cast<int32_t>(state); 148493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes if (state >= Thread::kTerminated && state <= Thread::kSuspended) { 148593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes os << kStateNames[int_state]; 1486b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers } else { 148793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes os << "State[" << int_state << "]"; 1488b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers } 1489b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers return os; 1490b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers} 1491b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers 1492330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughesstd::ostream& operator<<(std::ostream& os, const Thread& thread) { 149347179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes os << "Thread["; 149447179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes if (thread.GetThinLockId() != 0) { 149547179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes // If we're in kStarting, we won't have a thin lock id or tid yet. 149647179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes os << thread.GetThinLockId() 149747179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes << ",tid=" << thread.GetTid() << ','; 149847179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes } 149947179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes os << thread.GetState() 150047179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes << ",Thread*=" << &thread 150147179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes << ",Object*=" << thread.GetPeer() 15028daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes << "]"; 1503330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes return os; 1504330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes} 1505330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes 15068daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} // namespace art 1507