thread.cc revision f5a7a476e7ea63e094ff0f011dccc170607e6f6b
18d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes/* 28d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Copyright (C) 2011 The Android Open Source Project 38d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 48d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 58d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * you may not use this file except in compliance with the License. 68d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * You may obtain a copy of the License at 78d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 88d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 98d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 108d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Unless required by applicable law or agreed to in writing, software 118d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 128d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * See the License for the specific language governing permissions and 148d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * limitations under the License. 158d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes */ 16b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 17578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "thread.h" 18b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 198d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes#include <dynamic_annotations.h> 20b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers#include <pthread.h> 21b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers#include <sys/mman.h> 22a09576416788b916095739e43a16917e7948f3a4Elliott Hughes 23b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro#include <algorithm> 24dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes#include <bitset> 25eb4f614f2eb53b92ebd416fa418f550861655887Elliott Hughes#include <cerrno> 26a09576416788b916095739e43a16917e7948f3a4Elliott Hughes#include <iostream> 27b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro#include <list> 28b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 29a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes#include "class_linker.h" 30bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers#include "context.h" 31d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers#include "dex_verifier.h" 32408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers#include "heap.h" 33c5f7c91ab89055cffb573fff7e06dbdd860bccedElliott Hughes#include "jni_internal.h" 348e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes#include "monitor.h" 35a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes#include "object.h" 36578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "runtime.h" 375433072f589b61413e042eddf76e8190a048f71dbuzbee#include "runtime_support.h" 38aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#include "scoped_jni_thread_state.h" 3968e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack.h" 4068e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack_indirect_reference_table.h" 418daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "thread_list.h" 42a09576416788b916095739e43a16917e7948f3a4Elliott Hughes#include "utils.h" 43b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 44b557353b22c728eecbd1c68593b482622c7782a8Carl Shapironamespace art { 45b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 46b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiropthread_key_t Thread::pthread_key_self_; 47b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 488e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesstatic Class* gThreadLock = NULL; 4929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Class* gThrowable = NULL; 50038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_daemon = NULL; 51038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_group = NULL; 52038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_lock = NULL; 53038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_name = NULL; 54038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_priority = NULL; 5529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Field* gThread_uncaughtHandler = NULL; 56038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_vmData = NULL; 57038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThreadGroup_name = NULL; 588e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesstatic Field* gThreadLock_thread = NULL; 59038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Method* gThread_run = NULL; 6029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Method* gThreadGroup_removeThread = NULL; 6129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Method* gUncaughtExceptionHandler_uncaughtException = NULL; 62038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 63ce30293d222c864fa281da98bc896dd1c98a9a16buzbee// TODO: flesh out and move to appropriate location 64ce30293d222c864fa281da98bc896dd1c98a9a16buzbeeString* ResolveStringFromCode(Method* method, int32_t string_idx) { 6599f272349671e14ceada1cc795ce4c66a38ddd3ebuzbee UNIMPLEMENTED(FATAL) << "Resolve string; handle OOM"; 6699f272349671e14ceada1cc795ce4c66a38ddd3ebuzbee return NULL; // Must return valid string or if exception, doesn't return 6799f272349671e14ceada1cc795ce4c66a38ddd3ebuzbee} 6899f272349671e14ceada1cc795ce4c66a38ddd3ebuzbee 6999f272349671e14ceada1cc795ce4c66a38ddd3ebuzbee// TODO: move to appropriate location 7099f272349671e14ceada1cc795ce4c66a38ddd3ebuzbeestatic void ObjectInitFromCode(Object* o) { 7199f272349671e14ceada1cc795ce4c66a38ddd3ebuzbee Class* c = o->GetClass(); 7299f272349671e14ceada1cc795ce4c66a38ddd3ebuzbee if (c->IsFinalizable()) { 73adb460d96702573de6e732ad58ca0380405cd928Elliott Hughes Heap::AddFinalizerReference(o); 7499f272349671e14ceada1cc795ce4c66a38ddd3ebuzbee } 7599f272349671e14ceada1cc795ce4c66a38ddd3ebuzbee /* 7699f272349671e14ceada1cc795ce4c66a38ddd3ebuzbee * NOTE: once debugger/profiler support is added, we'll need to check 7799f272349671e14ceada1cc795ce4c66a38ddd3ebuzbee * here and branch to actual compiled object.<init> to handle any 7899f272349671e14ceada1cc795ce4c66a38ddd3ebuzbee * breakpoint/logging activites if either is active. 7999f272349671e14ceada1cc795ce4c66a38ddd3ebuzbee */ 80ce30293d222c864fa281da98bc896dd1c98a9a16buzbee} 81ce30293d222c864fa281da98bc896dd1c98a9a16buzbee 823ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbeevoid Thread::InitFunctionPointers() { 835433072f589b61413e042eddf76e8190a048f71dbuzbee#if defined(__arm__) 845433072f589b61413e042eddf76e8190a048f71dbuzbee pShlLong = art_shl_long; 855433072f589b61413e042eddf76e8190a048f71dbuzbee pShrLong = art_shr_long; 865433072f589b61413e042eddf76e8190a048f71dbuzbee pUshrLong = art_ushr_long; 877b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pIdiv = __aeabi_idiv; 887b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pIdivmod = __aeabi_idivmod; 897b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pI2f = __aeabi_i2f; 907b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pF2iz = __aeabi_f2iz; 917b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pD2f = __aeabi_d2f; 927b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pF2d = __aeabi_f2d; 937b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pD2iz = __aeabi_d2iz; 947b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pL2f = __aeabi_l2f; 957b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pL2d = __aeabi_l2d; 967b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFadd = __aeabi_fadd; 977b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFsub = __aeabi_fsub; 987b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFdiv = __aeabi_fdiv; 997b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFmul = __aeabi_fmul; 1007b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFmodf = fmodf; 1017b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pDadd = __aeabi_dadd; 1027b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pDsub = __aeabi_dsub; 1037b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pDdiv = __aeabi_ddiv; 1047b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pDmul = __aeabi_dmul; 1057b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pFmod = fmod; 1067b1b86d68244b0bb4ea3f43505eb45fdd46814d6buzbee pLdivmod = __aeabi_ldivmod; 107439c4fa0db980fb19e4a585723a64a3461e4c278buzbee pLmul = __aeabi_lmul; 10821d9e8323124a832a21679ca83808bc9c68ed365Ian Rogers pAllocObjectFromCode = art_alloc_object_from_code; 109b408de744566a5c5a80be1ba7f5c88407e816945Elliott Hughes pAllocArrayFromCode = art_alloc_array_from_code; 110e51a511ccee3f3c0120807321bcc160fcaa664beIan Rogers pCanPutArrayElementFromCode = art_can_put_array_element_from_code; 111b408de744566a5c5a80be1ba7f5c88407e816945Elliott Hughes pCheckAndAllocArrayFromCode = art_check_and_alloc_array_from_code; 112ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pCheckCastFromCode = art_check_cast_from_code; 113ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pHandleFillArrayDataFromCode = art_handle_fill_data_from_code; 114cbba6ac9bf9a6c630a7aafae6d8767b5ddbb6fd5Ian Rogers pInitializeStaticStorage = art_initialize_static_storage_from_code; 1154a3164faefd255b1c1e911e7ad7c3d57749caaf6buzbee pInvokeInterfaceTrampoline = art_invoke_interface_trampoline; 116c1f45048b90a85018c6b063c31bc088dc3dd993dbuzbee pTestSuspendFromCode = art_test_suspend; 117ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pThrowArrayBoundsFromCode = art_throw_array_bounds_from_code; 118ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pThrowDivZeroFromCode = art_throw_div_zero_from_code; 119c0c8dc8e677957d167066a2d06973126f108a6aaIan Rogers pThrowNegArraySizeFromCode = art_throw_neg_array_size_from_code; 120c0c8dc8e677957d167066a2d06973126f108a6aaIan Rogers pThrowNoSuchMethodFromCode = art_throw_no_such_method_from_code; 121ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pThrowNullPointerFromCode = art_throw_null_pointer_exception_from_code; 122932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers pThrowStackOverflowFromCode = art_throw_stack_overflow_from_code; 123c0c8dc8e677957d167066a2d06973126f108a6aaIan Rogers pThrowVerificationErrorFromCode = art_throw_verification_error_from_code; 1244f0d07c783afef89703dce32c94440fc8621a29bIan Rogers pLockObjectFromCode = art_lock_object_from_code; 125ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pUnlockObjectFromCode = art_unlock_object_from_code; 12667375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers#endif 127ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers pDeliverException = art_deliver_exception_from_code; 128c0c8dc8e677957d167066a2d06973126f108a6aaIan Rogers pThrowAbstractMethodErrorFromCode = ThrowAbstractMethodErrorFromCode; 129ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers pUnresolvedDirectMethodTrampolineFromCode = UnresolvedDirectMethodTrampolineFromCode; 130c396efc1baec875160afe34a3e65c46cce2bd72fbuzbee pF2l = F2L; 131c396efc1baec875160afe34a3e65c46cce2bd72fbuzbee pD2l = D2L; 1323ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee pMemcpy = memcpy; 133e1931749814dbb80c5a756f9842e9c261bb2e8f6buzbee pGet32Static = Field::Get32StaticFromCode; 134e1931749814dbb80c5a756f9842e9c261bb2e8f6buzbee pSet32Static = Field::Set32StaticFromCode; 135e1931749814dbb80c5a756f9842e9c261bb2e8f6buzbee pGet64Static = Field::Get64StaticFromCode; 136e1931749814dbb80c5a756f9842e9c261bb2e8f6buzbee pSet64Static = Field::Set64StaticFromCode; 137e1931749814dbb80c5a756f9842e9c261bb2e8f6buzbee pGetObjStatic = Field::GetObjStaticFromCode; 138e1931749814dbb80c5a756f9842e9c261bb2e8f6buzbee pSetObjStatic = Field::SetObjStaticFromCode; 1391b4c85959b3d9a4a33bc2160c46c1bbde67350c7buzbee pInitializeTypeFromCode = InitializeTypeFromCode; 140561227c80077bbb4147f778043f1a836af6b9248buzbee pResolveMethodFromCode = ResolveMethodFromCode; 141991e3ac0daf35d23f7c5e602d113c12ff3971544buzbee pInstanceofNonTrivialFromCode = Class::IsAssignableFromCode; 142845490bda68f7d025ea7f45775c847d2932e00dcBrian Carlstrom pFindInstanceFieldFromCode = Field::FindInstanceFieldFromCode; 1434f0d07c783afef89703dce32c94440fc8621a29bIan Rogers pCheckSuspendFromCode = artCheckSuspendFromJni; 144161928613d3f097108319de60494fab1aab8d48aBrian Carlstrom pFindNativeMethod = FindNativeMethod; 145161928613d3f097108319de60494fab1aab8d48aBrian Carlstrom pDecodeJObjectInThread = DecodeJObjectInThread; 146ce30293d222c864fa281da98bc896dd1c98a9a16buzbee pResolveStringFromCode = ResolveStringFromCode; 14799f272349671e14ceada1cc795ce4c66a38ddd3ebuzbee pObjectInit = ObjectInitFromCode; 1484a3164faefd255b1c1e911e7ad7c3d57749caaf6buzbee pDebugMe = DebugMe; 1493ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee} 1503ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee 15178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromvoid* Thread::CreateCallback(void* arg) { 15293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* self = reinterpret_cast<Thread*>(arg); 15393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Runtime* runtime = Runtime::Current(); 15493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 15593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes self->Attach(runtime); 15693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 157038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes String* thread_name = reinterpret_cast<String*>(gThread_name->GetObject(self->peer_)); 15893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes if (thread_name != NULL) { 15993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes SetThreadName(thread_name->ToModifiedUtf8().c_str()); 16093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes } 16193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 16293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Wait until it's safe to start running code. (There may have been a suspend-all 16393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // in progress while we were starting up.) 16493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes runtime->GetThreadList()->WaitForGo(); 16593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 16693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // TODO: say "hi" to the debugger. 16793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes //if (gDvm.debuggerConnected) { 16893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // dvmDbgPostThreadStart(self); 16993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes //} 17093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 17193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Invoke the 'run' method of our java.lang.Thread. 17293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes CHECK(self->peer_ != NULL); 17393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Object* receiver = self->peer_; 174038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes Method* m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(gThread_run); 17593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes m->Invoke(self, receiver, NULL, NULL); 17693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 17793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Detach. 17893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes runtime->GetThreadList()->Unregister(); 17993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 180b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro return NULL; 181b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 182b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 18393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughesvoid SetVmData(Object* managed_thread, Thread* native_thread) { 184038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes gThread_vmData->SetInt(managed_thread, reinterpret_cast<uintptr_t>(native_thread)); 18593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 18693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 18701158d7a57c8321370667a6045220237d16e0da8Elliott HughesThread* Thread::FromManagedThread(JNIEnv* env, jobject java_thread) { 18801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes Object* thread = Decode<Object*>(env, java_thread); 18901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes return reinterpret_cast<Thread*>(static_cast<uintptr_t>(gThread_vmData->GetInt(thread))); 19001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes} 19101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 1927502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughessize_t FixStackSize(size_t stack_size) { 1937502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // A stack size of zero means "use the default". 194d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (stack_size == 0) { 195d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes stack_size = Runtime::Current()->GetDefaultStackSize(); 196d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 19761e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 1987502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // It's not possible to request a stack smaller than the system-defined PTHREAD_STACK_MIN. 1997502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes if (stack_size < PTHREAD_STACK_MIN) { 2007502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = PTHREAD_STACK_MIN; 2017502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes } 2027502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2037502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // It's likely that callers are trying to ensure they have at least a certain amount of 2047502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // stack space, so we should add our reserved space on top of what they requested, rather 2057502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // than implicitly take it away from them. 2067502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size += Thread::kStackOverflowReservedBytes; 2077502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2087502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // Some systems require the stack size to be a multiple of the system page size, so round up. 2097502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = RoundUp(stack_size, kPageSize); 2107502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2117502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes return stack_size; 2127502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes} 2137502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2147502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughesvoid Thread::Create(Object* peer, size_t stack_size) { 2157502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes CHECK(peer != NULL); 2167502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2177502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = FixStackSize(stack_size); 2187502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 21993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* native_thread = new Thread; 22093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes native_thread->peer_ = peer; 22193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 22293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Thread.start is synchronized, so we know that vmData is 0, 22393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // and know that we're not racing to assign it. 22493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes SetVmData(peer, native_thread); 22561e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 22661e019d291583029c01b61b93bea750f2b663c37Carl Shapiro pthread_attr_t attr; 2278d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread"); 2288d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), "PTHREAD_CREATE_DETACHED"); 2298d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size); 2308d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_create, (&native_thread->pthread_, &attr, Thread::CreateCallback, native_thread), "new thread"); 2318d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread"); 23261e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 23393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Let the child know when it's safe to start running. 23493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Runtime::Current()->GetThreadList()->SignalGo(native_thread); 23593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 23661e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 23793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughesvoid Thread::Attach(const Runtime* runtime) { 23893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitCpu(); 23993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitFunctionPointers(); 24061e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 24193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes thin_lock_id_ = Runtime::Current()->GetThreadList()->AllocThreadId(); 242be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 24393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes tid_ = ::art::GetTid(); 24493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes pthread_ = pthread_self(); 24561e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 24693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitStackHwm(); 247dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 2488d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach"); 249a5780dad67556297c8ca5f2608c53b193e6c4514Elliott Hughes 25093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes jni_env_ = new JNIEnvExt(this, runtime->GetJavaVM()); 2515fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 2527a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes runtime->GetThreadList()->Register(); 25393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 25493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 25593e74e8d879270071c3aa163f8495ada8d21f42fElliott HughesThread* Thread::Attach(const Runtime* runtime, const char* name, bool as_daemon) { 25693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* self = new Thread; 25793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes self->Attach(runtime); 25893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 2597a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes self->SetState(Thread::kNative); 26093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 26193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes SetThreadName(name); 2625fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 2635fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes // If we're the main thread, ClassLinker won't be created until after we're attached, 2645fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes // so that thread needs a two-stage attach. Regular threads don't need this hack. 2655fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes if (self->thin_lock_id_ != ThreadList::kMainId) { 2665fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes self->CreatePeer(name, as_daemon); 2675fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes } 2685fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 2695fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes return self; 2705fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes} 2715fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 272d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughesjobject GetWellKnownThreadGroup(JNIEnv* env, const char* field_name) { 273d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes jclass thread_group_class = env->FindClass("java/lang/ThreadGroup"); 274d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes jfieldID fid = env->GetStaticFieldID(thread_group_class, field_name, "Ljava/lang/ThreadGroup;"); 275d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes jobject thread_group = env->GetStaticObjectField(thread_group_class, fid); 276d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes // This will be null in the compiler (and tests), but never in a running system. 277d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes //CHECK(thread_group != NULL) << "java.lang.ThreadGroup." << field_name << " not initialized"; 278d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes return thread_group; 279d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes} 280d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 2815fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughesvoid Thread::CreatePeer(const char* name, bool as_daemon) { 2825fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes JNIEnv* env = jni_env_; 2835fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 284d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes const char* field_name = (GetThinLockId() == ThreadList::kMainId) ? "mMain" : "mSystem"; 285d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes jobject thread_group = GetWellKnownThreadGroup(env, field_name); 2865fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes jobject thread_name = env->NewStringUTF(name); 2878daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes jint thread_priority = GetNativePriority(); 2885fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes jboolean thread_is_daemon = as_daemon; 2895fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 2905fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes jclass c = env->FindClass("java/lang/Thread"); 2915fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V"); 292330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes 2938daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes jobject peer = env->NewObject(c, mid, thread_group, thread_name, thread_priority, thread_is_daemon); 29401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes peer_ = DecodeJObject(peer); 2957a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes SetVmData(peer_, Thread::Current()); 296d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 297d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes // Because we mostly run without code available (in the compiler, in tests), we 298d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes // manually assign the fields the constructor should have set. 299d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes // TODO: lose this. 30001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes gThread_daemon->SetBoolean(peer_, thread_is_daemon); 30101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes gThread_group->SetObject(peer_, Decode<Object*>(env, thread_group)); 30201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes gThread_name->SetObject(peer_, Decode<Object*>(env, thread_name)); 30301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes gThread_priority->SetInt(peer_, thread_priority); 30461e019d291583029c01b61b93bea750f2b663c37Carl Shapiro} 30561e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 306be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::InitStackHwm() { 307be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes pthread_attr_t attributes; 3088d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_getattr_np, (pthread_, &attributes), __FUNCTION__); 309be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 310932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers void* temp_stack_base; 311932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, &temp_stack_base, &stack_size_), 312932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers __FUNCTION__); 313932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers stack_base_ = reinterpret_cast<byte*>(temp_stack_base); 314be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 315932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers if (stack_size_ <= kStackOverflowReservedBytes) { 316932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers LOG(FATAL) << "attempt to attach a thread with a too-small stack (" << stack_size_ << " bytes)"; 317be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 318449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes 319932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // Set stack_end_ to the bottom of the stack saving space of stack overflows 320932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers ResetDefaultStackEnd(); 321449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes 322449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes // Sanity check. 323449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes int stack_variable; 324449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes CHECK_GT(&stack_variable, (void*) stack_end_); 325be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 3268d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); 327be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes} 328be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 329a09576416788b916095739e43a16917e7948f3a4Elliott Hughesvoid Thread::Dump(std::ostream& os) const { 330d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes DumpState(os); 331d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes DumpStack(os); 332d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes} 333d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 334d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesstd::string GetSchedulerGroup(pid_t tid) { 335d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // /proc/<pid>/group looks like this: 336d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // 2:devices:/ 337d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // 1:cpuacct,cpu:/ 338d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // We want the third field from the line whose second field contains the "cpu" token. 339d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string cgroup_file; 340d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (!ReadFileToString("/proc/self/cgroup", &cgroup_file)) { 341d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes return ""; 342d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 343d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::vector<std::string> cgroup_lines; 344d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes Split(cgroup_file, '\n', cgroup_lines); 345d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes for (size_t i = 0; i < cgroup_lines.size(); ++i) { 346d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::vector<std::string> cgroup_fields; 347d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes Split(cgroup_lines[i], ':', cgroup_fields); 348d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::vector<std::string> cgroups; 349d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes Split(cgroup_fields[1], ',', cgroups); 350d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes for (size_t i = 0; i < cgroups.size(); ++i) { 351d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (cgroups[i] == "cpu") { 352d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes return cgroup_fields[2].substr(1); // Skip the leading slash. 353d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 354d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 355d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 356d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes return ""; 357d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes} 358d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 359d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesvoid Thread::DumpState(std::ostream& os) const { 360d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::string thread_name("<native thread without managed peer>"); 361d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::string group_name; 362d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes int priority; 363d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes bool is_daemon = false; 364d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 365d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (peer_ != NULL) { 366038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes String* thread_name_string = reinterpret_cast<String*>(gThread_name->GetObject(peer_)); 367d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes thread_name = (thread_name_string != NULL) ? thread_name_string->ToModifiedUtf8() : "<null>"; 368038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes priority = gThread_priority->GetInt(peer_); 369038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes is_daemon = gThread_daemon->GetBoolean(peer_); 370d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 371038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes Object* thread_group = gThread_group->GetObject(peer_); 372d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (thread_group != NULL) { 373038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes String* group_name_string = reinterpret_cast<String*>(gThreadGroup_name->GetObject(thread_group)); 374d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes group_name = (group_name_string != NULL) ? group_name_string->ToModifiedUtf8() : "<null>"; 375d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 376d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } else { 377d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes // This name may be truncated, but it's the best we can do in the absence of a managed peer. 378dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes std::string stats; 379dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes if (ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { 380dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes size_t start = stats.find('(') + 1; 381dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes size_t end = stats.find(')') - start; 382dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes thread_name = stats.substr(start, end); 383dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes } 384d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes priority = GetNativePriority(); 385dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes } 386d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 387d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int policy; 388d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes sched_param sp; 3898d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_getschedparam, (pthread_, &policy, &sp), __FUNCTION__); 390d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 391d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string scheduler_group(GetSchedulerGroup(GetTid())); 392d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (scheduler_group.empty()) { 393d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_group = "default"; 394d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 395d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 396d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << '"' << thread_name << '"'; 397d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (is_daemon) { 398d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " daemon"; 399d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 400d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " prio=" << priority 401dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes << " tid=" << GetThinLockId() 40293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes << " " << GetState() << "\n"; 403d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 404d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int debug_suspend_count = 0; // TODO 405d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | group=\"" << group_name << "\"" 4068d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes << " sCount=" << suspend_count_ 407d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " dsCount=" << debug_suspend_count 408dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes << " obj=" << reinterpret_cast<void*>(peer_) 409d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " self=" << reinterpret_cast<const void*>(this) << "\n"; 410d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | sysTid=" << GetTid() 411d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " nice=" << getpriority(PRIO_PROCESS, GetTid()) 412d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " sched=" << policy << "/" << sp.sched_priority 413d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " cgrp=" << scheduler_group 414d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " handle=" << GetImpl() << "\n"; 415d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 416d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // Grab the scheduler stats for this thread. 417d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string scheduler_stats; 418d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", GetTid()).c_str(), &scheduler_stats)) { 419d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'. 420d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } else { 421d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_stats = "0 0 0"; 422d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 423d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 424d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int utime = 0; 425d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int stime = 0; 426d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int task_cpu = 0; 427d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string stats; 428d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { 429d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // Skip the command, which may contain spaces. 430d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes stats = stats.substr(stats.find(')') + 2); 431d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // Extract the three fields we care about. 432d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::vector<std::string> fields; 433d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes Split(stats, ' ', fields); 434d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes utime = strtoull(fields[11].c_str(), NULL, 10); 435d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes stime = strtoull(fields[12].c_str(), NULL, 10); 436d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes task_cpu = strtoull(fields[36].c_str(), NULL, 10); 437d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 438d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 439d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | schedstat=( " << scheduler_stats << " )" 440d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " utm=" << utime 441d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " stm=" << stime 442d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " core=" << task_cpu 443d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " HZ=" << sysconf(_SC_CLK_TCK) << "\n"; 444a09576416788b916095739e43a16917e7948f3a4Elliott Hughes} 445a09576416788b916095739e43a16917e7948f3a4Elliott Hughes 446d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughesstruct StackDumpVisitor : public Thread::StackVisitor { 4478e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes StackDumpVisitor(std::ostream& os, const Thread* thread) 4488e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes : os(os), thread(thread), frame_count(0) { 449d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 450d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 451bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers virtual ~StackDumpVisitor() { 452d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 453d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 454bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers void VisitFrame(const Frame& frame, uintptr_t pc) { 4559086572fa809d1a19d886b467e4da3ce42016982Ian Rogers if (!frame.HasMethod()) { 4569086572fa809d1a19d886b467e4da3ce42016982Ian Rogers return; 4579086572fa809d1a19d886b467e4da3ce42016982Ian Rogers } 458d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 459d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Method* m = frame.GetMethod(); 460d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Class* c = m->GetDeclaringClass(); 4618e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 462d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes const DexFile& dex_file = class_linker->FindDexFile(c->GetDexCache()); 463d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 464d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes os << " at " << PrettyMethod(m, false); 465d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (m->IsNative()) { 466d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes os << "(Native method)"; 467d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } else { 468bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers int line_number = dex_file.GetLineNumFromPC(m, m->ToDexPC(pc)); 469d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes os << "(" << c->GetSourceFile()->ToModifiedUtf8() << ":" << line_number << ")"; 470d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 471d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes os << "\n"; 4728e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 4738e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (frame_count++ == 0) { 4748e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Monitor::DescribeWait(os, thread); 4758e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 476d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 477d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 478d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::ostream& os; 4798e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes const Thread* thread; 4808e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes int frame_count; 481d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes}; 482d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 483d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesvoid Thread::DumpStack(std::ostream& os) const { 4848e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes StackDumpVisitor dumper(os, this); 485d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes WalkStack(&dumper); 486e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes} 487e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes 4888d768a954b101a9532f980253ac46be2c53aba11Elliott HughesThread::State Thread::SetState(Thread::State new_state) { 4898d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes Thread::State old_state = state_; 4908d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (old_state == new_state) { 4918d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes return old_state; 4928d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 4938d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 4948d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes volatile void* raw = reinterpret_cast<volatile void*>(&state_); 4958d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes volatile int32_t* addr = reinterpret_cast<volatile int32_t*>(raw); 4968d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 4978d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (new_state == Thread::kRunnable) { 4988d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes /* 4998d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Change our status to Thread::kRunnable. The transition requires 5008d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * that we check for pending suspension, because the VM considers 5018d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * us to be "asleep" in all other states, and another thread could 5028d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * be performing a GC now. 5038d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5048d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * The order of operations is very significant here. One way to 5058d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * do this wrong is: 5068d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5078d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * GCing thread Our thread (in kNative) 5088d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * ------------ ---------------------- 5098d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * check suspend count (== 0) 5108d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * SuspendAllThreads() 5118d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * grab suspend-count lock 5128d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * increment all suspend counts 5138d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * release suspend-count lock 5148d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * check thread state (== kNative) 5158d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * all are suspended, begin GC 5168d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * set state to kRunnable 5178d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * (continue executing) 5188d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5198d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We can correct this by grabbing the suspend-count lock and 5208d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * performing both of our operations (check suspend count, set 5218d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * state) while holding it, now we need to grab a mutex on every 5228d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * transition to kRunnable. 5238d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5248d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * What we do instead is change the order of operations so that 5258d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * the transition to kRunnable happens first. If we then detect 5268d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * that the suspend count is nonzero, we switch to kSuspended. 5278d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5288d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Appropriate compiler and memory barriers are required to ensure 5298d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * that the operations are observed in the expected order. 5308d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5318d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * This does create a small window of opportunity where a GC in 5328d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * progress could observe what appears to be a running thread (if 5338d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * it happens to look between when we set to kRunnable and when we 5348d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * switch to kSuspended). At worst this only affects assertions 5358d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * and thread logging. (We could work around it with some sort 5368d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * of intermediate "pre-running" state that is generally treated 5378d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * as equivalent to running, but that doesn't seem worthwhile.) 5388d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5398d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We can also solve this by combining the "status" and "suspend 5408d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * count" fields into a single 32-bit value. This trades the 5418d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * store/load barrier on transition to kRunnable for an atomic RMW 5428d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * op on all transitions and all suspend count updates (also, all 5438d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * accesses to status or the thread count require bit-fiddling). 5448d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * It also eliminates the brief transition through kRunnable when 5458d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * the thread is supposed to be suspended. This is possibly faster 5468d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * on SMP and slightly more correct, but less convenient. 5478d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes */ 5488d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes android_atomic_acquire_store(new_state, addr); 5498d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (ANNOTATE_UNPROTECTED_READ(suspend_count_) != 0) { 5508d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes Runtime::Current()->GetThreadList()->FullSuspendCheck(this); 5518d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 5528d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } else { 5538d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes /* 5548d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Not changing to Thread::kRunnable. No additional work required. 5558d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5568d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We use a releasing store to ensure that, if we were runnable, 5578d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * any updates we previously made to objects on the managed heap 5588d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * will be observed before the state change. 5598d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes */ 5608d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes android_atomic_release_store(new_state, addr); 5618d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 5628d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 5638d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes return old_state; 5648d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes} 5658d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 5668d768a954b101a9532f980253ac46be2c53aba11Elliott Hughesvoid Thread::WaitUntilSuspended() { 5678d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes // TODO: dalvik dropped the waiting thread's priority after a while. 5688d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes // TODO: dalvik timed out and aborted. 5698d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes useconds_t delay = 0; 5708d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes while (GetState() == Thread::kRunnable) { 5718d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes useconds_t new_delay = delay * 2; 5728d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_GE(new_delay, delay); 5738d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes delay = new_delay; 5748d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (delay == 0) { 5758d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes sched_yield(); 5768d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes delay = 10000; 5778d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } else { 5788d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes usleep(delay); 5798d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 5808d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 5818d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes} 5828d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 583be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::ThreadExitCallback(void* arg) { 584be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes Thread* self = reinterpret_cast<Thread*>(arg); 585be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self; 586b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 587b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 588be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::Startup() { 589b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro // Allocate a TLS slot. 5908d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_key_create, (&Thread::pthread_key_self_, Thread::ThreadExitCallback), "self key"); 591b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 592b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro // Double-check the TLS slot allocation. 593b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro if (pthread_getspecific(pthread_key_self_) != NULL) { 594be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes LOG(FATAL) << "newly-created pthread TLS slot is not NULL"; 595b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro } 596038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes} 597038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 5988e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 5998e4aac52962d54cb4be2078b9cd95685e067133aElliott HughesClass* FindPrimitiveClassOrDie(ClassLinker* class_linker, char descriptor) { 6008e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* c = class_linker->FindPrimitiveClass(descriptor); 6018e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(c != NULL) << descriptor; 6028e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return c; 6038e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 6048e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6058e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 6068e4aac52962d54cb4be2078b9cd95685e067133aElliott HughesClass* FindClassOrDie(ClassLinker* class_linker, const char* descriptor) { 6078e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* c = class_linker->FindSystemClass(descriptor); 6088e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(c != NULL) << descriptor; 6098e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return c; 6108e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 6118e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6128e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 6138e4aac52962d54cb4be2078b9cd95685e067133aElliott HughesField* FindFieldOrDie(Class* c, const char* name, Class* type) { 6148e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Field* f = c->FindDeclaredInstanceField(name, type); 6158e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(f != NULL) << PrettyClass(c) << " " << name << " " << PrettyClass(type); 6168e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return f; 6178e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 6188e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6198e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 6208e4aac52962d54cb4be2078b9cd95685e067133aElliott HughesMethod* FindMethodOrDie(Class* c, const char* name, const char* signature) { 6218e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Method* m = c->FindVirtualMethod(name, signature); 6228e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(m != NULL) << PrettyClass(c) << " " << name << " " << signature; 6238e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return m; 6248e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 6258e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 626038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesvoid Thread::FinishStartup() { 627038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes // Now the ClassLinker is ready, we can find the various Class*, Field*, and Method*s we need. 628038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 6298e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6308e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* boolean_class = FindPrimitiveClassOrDie(class_linker, 'Z'); 6318e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* int_class = FindPrimitiveClassOrDie(class_linker, 'I'); 6328e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* String_class = FindClassOrDie(class_linker, "Ljava/lang/String;"); 6338e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* Thread_class = FindClassOrDie(class_linker, "Ljava/lang/Thread;"); 6348e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* ThreadGroup_class = FindClassOrDie(class_linker, "Ljava/lang/ThreadGroup;"); 6358e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* UncaughtExceptionHandler_class = FindClassOrDie(class_linker, "Ljava/lang/Thread$UncaughtExceptionHandler;"); 6368e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThreadLock = FindClassOrDie(class_linker, "Ljava/lang/ThreadLock;"); 6378e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThrowable = FindClassOrDie(class_linker, "Ljava/lang/Throwable;"); 6388e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6398e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_daemon = FindFieldOrDie(Thread_class, "daemon", boolean_class); 6408e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_group = FindFieldOrDie(Thread_class, "group", ThreadGroup_class); 6418e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_lock = FindFieldOrDie(Thread_class, "lock", gThreadLock); 6428e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_name = FindFieldOrDie(Thread_class, "name", String_class); 6438e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_priority = FindFieldOrDie(Thread_class, "priority", int_class); 6448e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_uncaughtHandler = FindFieldOrDie(Thread_class, "uncaughtHandler", UncaughtExceptionHandler_class); 6458e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_vmData = FindFieldOrDie(Thread_class, "vmData", int_class); 6468e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThreadGroup_name = FindFieldOrDie(ThreadGroup_class, "name", String_class); 6478e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThreadLock_thread = FindFieldOrDie(gThreadLock, "thread", Thread_class); 6488e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 6498e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_run = FindMethodOrDie(Thread_class, "run", "()V"); 6508e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThreadGroup_removeThread = FindMethodOrDie(ThreadGroup_class, "removeThread", "(Ljava/lang/Thread;)V"); 6518e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gUncaughtExceptionHandler_uncaughtException = FindMethodOrDie(UncaughtExceptionHandler_class, 6528e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes "uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V"); 65301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 65401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Finish attaching the main thread. 65501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes Thread::Current()->CreatePeer("main", false); 656b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 657b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 658c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughesvoid Thread::Shutdown() { 6598d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key"); 660c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes} 661c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes 6628e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesuint32_t Thread::LockOwnerFromThreadLock(Object* thread_lock) { 6638e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (thread_lock == NULL || thread_lock->GetClass() != gThreadLock) { 6648e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 6658e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 6668e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Object* managed_thread = gThreadLock_thread->GetObject(thread_lock); 6678e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (managed_thread == NULL) { 6688e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 6698e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 6708e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes uintptr_t vmData = static_cast<uintptr_t>(gThread_vmData->GetInt(managed_thread)); 6718e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Thread* thread = reinterpret_cast<Thread*>(vmData); 6728e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (thread == NULL) { 6738e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 6748e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 6758e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return thread->GetThinLockId(); 6768e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 6778e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 678dcc247493fd8fb243e335c3ec08e5e625896a47cElliott HughesThread::Thread() 67902b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes : peer_(NULL), 6808e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes top_of_managed_stack_(), 6818e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes top_of_managed_stack_pc_(0), 68285d1545e985ac689db4bad7849880e843707c862Elliott Hughes wait_mutex_(new Mutex("Thread wait mutex")), 68385d1545e985ac689db4bad7849880e843707c862Elliott Hughes wait_cond_(new ConditionVariable("Thread wait condition variable")), 6848daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes wait_monitor_(NULL), 6858daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes interrupted_(false), 686dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes wait_next_(NULL), 6878e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes monitor_enter_object_(NULL), 688dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes card_table_(0), 6898daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes stack_end_(NULL), 690dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes native_to_managed_record_(NULL), 691dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes top_sirt_(NULL), 692dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes jni_env_(NULL), 6938e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes state_(Thread::kNative), 694dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes self_(NULL), 695dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes runtime_(NULL), 696dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes exception_(NULL), 697dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes suspend_count_(0), 69885d1545e985ac689db4bad7849880e843707c862Elliott Hughes class_loader_override_(NULL), 699418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes long_jump_context_(NULL), 700418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes throwing_OOME_(false) { 701f5a7a476e7ea63e094ff0f011dccc170607e6f6bElliott Hughes CHECK_EQ((sizeof(Thread) % 4), 0U) << sizeof(Thread); 702dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes} 703dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 70402b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughesvoid MonitorExitVisitor(const Object* object, void*) { 70502b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes Object* entered_monitor = const_cast<Object*>(object); 7065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes entered_monitor->MonitorExit(Thread::Current()); 70702b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes} 70802b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 709c1674ed06662420213441ff2b818f2f71f9098dcElliott HughesThread::~Thread() { 7107a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes SetState(Thread::kRunnable); 7117a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes 71202b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited. 71393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes if (jni_env_ != NULL) { 71493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes jni_env_->monitors.VisitRoots(MonitorExitVisitor, NULL); 71593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes } 71602b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 71729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (peer_ != NULL) { 71829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes Object* group = gThread_group->GetObject(peer_); 71929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 72029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // Handle any pending exception. 72129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (IsExceptionPending()) { 72229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // Get and clear the exception. 72329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes Object* exception = GetException(); 72429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes ClearException(); 72529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 72629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // If the thread has its own handler, use that. 72729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes Object* handler = gThread_uncaughtHandler->GetObject(peer_); 72829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (handler == NULL) { 72929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // Otherwise use the thread group's default handler. 73029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes handler = group; 73129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 73202b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 73329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // Call the handler. 73429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes Method* m = handler->GetClass()->FindVirtualMethodForVirtualOrInterface(gUncaughtExceptionHandler_uncaughtException); 73529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes Object* args[2]; 73629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes args[0] = peer_; 73729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes args[1] = exception; 73829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes m->Invoke(this, handler, reinterpret_cast<byte*>(&args), NULL); 73902b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 74029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // If the handler threw, clear that exception too. 74129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes ClearException(); 74229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 74329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 74429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // this.group.removeThread(this); 745081be7fcd72490937e188e9ef56e72df06b7b006Elliott Hughes // group can be null if we're in the compiler or a test. 746081be7fcd72490937e188e9ef56e72df06b7b006Elliott Hughes if (group != NULL) { 747081be7fcd72490937e188e9ef56e72df06b7b006Elliott Hughes Method* m = group->GetClass()->FindVirtualMethodForVirtualOrInterface(gThreadGroup_removeThread); 748081be7fcd72490937e188e9ef56e72df06b7b006Elliott Hughes Object* args = peer_; 749081be7fcd72490937e188e9ef56e72df06b7b006Elliott Hughes m->Invoke(this, group, reinterpret_cast<byte*>(&args), NULL); 750081be7fcd72490937e188e9ef56e72df06b7b006Elliott Hughes } 75129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 75229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // this.vmData = 0; 75393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes SetVmData(peer_, NULL); 75402b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 75529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // TODO: say "bye" to the debugger. 75629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes //if (gDvm.debuggerConnected) { 75729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // dvmDbgPostThreadDeath(self); 75829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes //} 75902b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 76029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // Thread.join() is implemented as an Object.wait() on the Thread.lock 76129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // object. Signal anyone who is waiting. 7625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* self = Thread::Current(); 763038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes Object* lock = gThread_lock->GetObject(peer_); 764038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes // (This conditional is only needed for tests, where Thread.lock won't have been set.) 7655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (lock != NULL) { 7665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->MonitorEnter(self); 7675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->NotifyAll(); 7685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->MonitorExit(self); 7695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 77102b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 772c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes delete jni_env_; 77302b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes jni_env_ = NULL; 77402b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 77502b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes SetState(Thread::kTerminated); 77685d1545e985ac689db4bad7849880e843707c862Elliott Hughes 77785d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete wait_cond_; 77885d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete wait_mutex_; 77985d1545e985ac689db4bad7849880e843707c862Elliott Hughes 78085d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete long_jump_context_; 781c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes} 782c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes 783408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogerssize_t Thread::NumSirtReferences() { 784a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers size_t count = 0; 785408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { 786a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers count += cur->NumberOfReferences(); 787a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 788a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return count; 789a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers} 790a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers 791408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogersbool Thread::SirtContains(jobject obj) { 792408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers Object** sirt_entry = reinterpret_cast<Object**>(obj); 793408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { 794a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers size_t num_refs = cur->NumberOfReferences(); 795408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // A SIRT should always have a jobject/jclass as a native method is passed 796408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // in a this pointer or a class 797408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers DCHECK_GT(num_refs, 0u); 7982f0ce9d60a4a9371c63a32a3764320fb02341acbShih-wei Liao if ((&cur->References()[0] <= sirt_entry) && 7992f0ce9d60a4a9371c63a32a3764320fb02341acbShih-wei Liao (sirt_entry <= (&cur->References()[num_refs - 1]))) { 800a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return true; 801a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 802a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 803a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return false; 804a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers} 805a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers 8068dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liaovoid Thread::SirtVisitRoots(Heap::RootVisitor* visitor, void* arg) { 8078dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { 8088dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao size_t num_refs = cur->NumberOfReferences(); 8098dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao for (size_t j = 0; j < num_refs; j++) { 8108dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao visitor(cur->References()[j], arg); 8118dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao } 8128dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao } 8138dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao} 8148dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 81567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogersvoid Thread::PopSirt() { 81667375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers CHECK(top_sirt_ != NULL); 81767375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers top_sirt_ = top_sirt_->Link(); 81867375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers} 81967375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers 820408f79aeb676251ba35667a64e86c20638d7cb0bIan RogersObject* Thread::DecodeJObject(jobject obj) { 8210cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers DCHECK(CanAccessDirectReferences()); 822408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (obj == NULL) { 823408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return NULL; 824408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 825408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectRef ref = reinterpret_cast<IndirectRef>(obj); 826408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectRefKind kind = GetIndirectRefKind(ref); 827408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers Object* result; 828408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers switch (kind) { 829408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kLocal: 830408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 83169f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughes IndirectReferenceTable& locals = jni_env_->locals; 832cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(locals.Get(ref)); 833408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 834408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 835408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kGlobal: 836408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 837408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers JavaVMExt* vm = Runtime::Current()->GetJavaVM(); 838408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectReferenceTable& globals = vm->globals; 839408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers MutexLock mu(vm->globals_lock); 840cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(globals.Get(ref)); 841408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 842408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 843408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kWeakGlobal: 844408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 845408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers JavaVMExt* vm = Runtime::Current()->GetJavaVM(); 846408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectReferenceTable& weak_globals = vm->weak_globals; 847408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers MutexLock mu(vm->weak_globals_lock); 848cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(weak_globals.Get(ref)); 849408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (result == kClearedJniWeakGlobal) { 850408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // This is a special case where it's okay to return NULL. 851408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return NULL; 852408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 853408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 854408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 855408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kSirtOrInvalid: 856408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers default: 857408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // TODO: make stack indirect reference table lookup more efficient 858408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Check if this is a local reference in the SIRT 859408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (SirtContains(obj)) { 8600cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers result = *reinterpret_cast<Object**>(obj); // Read from SIRT 861c5bfa8f49d8548d7c685a99b411311ef56bedffaElliott Hughes } else if (jni_env_->work_around_app_jni_bugs) { 862408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Assume an invalid local reference is actually a direct pointer. 863408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers result = reinterpret_cast<Object*>(obj); 864408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } else { 865a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes result = kInvalidIndirectRefObject; 866408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 867408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 868408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 869408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (result == NULL) { 870a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes LOG(ERROR) << "JNI ERROR (app bug): use of deleted " << kind << ": " << obj; 871a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes JniAbort(NULL); 872a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes } else { 873a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes if (result != kInvalidIndirectRefObject) { 874a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes Heap::VerifyObject(result); 875a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes } 876408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 877408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return result; 878408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers} 879408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 8809b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liaoclass CountStackDepthVisitor : public Thread::StackVisitor { 8819b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 88229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes CountStackDepthVisitor() : depth_(0), skip_depth_(0), skipping_(true) {} 883d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 88429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes virtual void VisitFrame(const Frame& frame, uintptr_t pc) { 88529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // We want to skip frames up to and including the exception's constructor. 8869086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // Note we also skip the frame if it doesn't have a method (namely the callee 8879086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // save frame) 88825c3325bf95036bf325fc7cb21b4fd6d40282857Brian Carlstrom DCHECK(gThrowable != NULL); 8899086572fa809d1a19d886b467e4da3ce42016982Ian Rogers if (skipping_ && frame.HasMethod() && !gThrowable->IsAssignableFrom(frame.GetMethod()->GetDeclaringClass())) { 89029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes skipping_ = false; 89129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 89229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (!skipping_) { 89329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes ++depth_; 89429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } else { 89529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes ++skip_depth_; 89629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 8979b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 8989b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 8999b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao int GetDepth() const { 900aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return depth_; 9019b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9029b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 90329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int GetSkipDepth() const { 90429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes return skip_depth_; 90529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 90629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 9079b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao private: 908aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t depth_; 90929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes uint32_t skip_depth_; 91029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes bool skipping_; 9119b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao}; 9129b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 913aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogersclass BuildInternalStackTraceVisitor : public Thread::StackVisitor { 9149b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 91529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes explicit BuildInternalStackTraceVisitor(int depth, int skip_depth, ScopedJniThreadState& ts) 91629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes : skip_depth_(skip_depth), count_(0) { 917aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Allocate method trace with an extra slot that will hold the PC trace 91801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes method_trace_ = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(depth + 1); 919aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Register a local reference as IntArray::Alloc may trigger GC 920aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers local_ref_ = AddLocalReference<jobject>(ts.Env(), method_trace_); 921aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers pc_trace_ = IntArray::Alloc(depth); 922aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#ifdef MOVING_GARBAGE_COLLECTOR 923aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Re-read after potential GC 924aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace = Decode<ObjectArray<Object>*>(ts.Env(), local_ref_); 925aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#endif 926aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Save PC trace in last element of method trace, also places it into the 927aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // object graph. 928aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace_->Set(depth, pc_trace_); 9299b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9309b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 931aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers virtual ~BuildInternalStackTraceVisitor() {} 9329b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 933bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers virtual void VisitFrame(const Frame& frame, uintptr_t pc) { 93429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (skip_depth_ > 0) { 93529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes skip_depth_--; 93629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes return; 93729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 938aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace_->Set(count_, frame.GetMethod()); 939bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers pc_trace_->Set(count_, pc); 940aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ++count_; 9419b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9429b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 943aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers jobject GetInternalStackTrace() const { 944aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return local_ref_; 9459b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9469b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 9479b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao private: 94829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // How many more frames to skip. 94929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int32_t skip_depth_; 950aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Current position down stack trace 951aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t count_; 952aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Array of return PC values 953aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers IntArray* pc_trace_; 954aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // An array of the methods on the stack, the last entry is a reference to the 955aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // PC trace 956aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ObjectArray<Object>* method_trace_; 957aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Local indirect reference table entry for method trace 958aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers jobject local_ref_; 9599b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao}; 9609b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 961a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes// TODO: remove this. 962a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughesuintptr_t ManglePc(uintptr_t pc) { 963a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // Move the PC back 2 bytes as a call will frequently terminate the 964a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // decoding of a particular instruction and we want to make sure we 965a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // get the Dex PC of the instruction with the call and not the 966a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // instruction following. 967a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes if (pc > 0) { pc -= 2; } 968a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes return pc; 969a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes} 970a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes 971a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes// TODO: remove this. 972a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughesuintptr_t DemanglePc(uintptr_t pc) { 973a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // Revert mangling for the case where we need the PC to return to the upcall 974a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes return pc + 2; 975a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes} 976f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers 977aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogersvoid Thread::WalkStack(StackVisitor* visitor) const { 978d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Frame frame = GetTopOfStack(); 979a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes uintptr_t pc = ManglePc(top_of_managed_stack_pc_); 9809b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao // TODO: enable this CHECK after native_to_managed_record_ is initialized during startup. 9819b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao // CHECK(native_to_managed_record_ != NULL); 9829b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao NativeToManagedRecord* record = native_to_managed_record_; 9834417536522fd2a9d8215d8672331984769c9520bShih-wei Liao 984bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers while (frame.GetSP() != 0) { 9859b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao for ( ; frame.GetMethod() != 0; frame.Next()) { 9863320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom // DCHECK(frame.GetMethod()->IsWithinCode(pc)); // TODO: restore IsWithinCode 9873ddac99d4dc6a036fac59d8f0bdc664ef619fb04Shih-wei Liao visitor->VisitFrame(frame, pc); 988a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes pc = ManglePc(frame.GetReturnPC()); 9899b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 9909b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao if (record == NULL) { 9919b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao break; 9929b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 993bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // last_tos should return Frame instead of sp? 994ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers frame.SetSP(reinterpret_cast<Method**>(record->last_top_of_managed_stack_)); 995a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes pc = ManglePc(record->last_top_of_managed_stack_pc_); 996bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers record = record->link_; 997bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 998bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers} 999bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 100067375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogersvoid Thread::WalkStackUntilUpCall(StackVisitor* visitor, bool include_upcall) const { 1001bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Frame frame = GetTopOfStack(); 1002a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes uintptr_t pc = ManglePc(top_of_managed_stack_pc_); 1003bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1004bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (frame.GetSP() != 0) { 1005bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers for ( ; frame.GetMethod() != 0; frame.Next()) { 10063320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom // DCHECK(frame.GetMethod()->IsWithinCode(pc)); // TODO: restore IsWithinCode 10073ddac99d4dc6a036fac59d8f0bdc664ef619fb04Shih-wei Liao visitor->VisitFrame(frame, pc); 1008a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes pc = ManglePc(frame.GetReturnPC()); 1009bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 101067375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers if (include_upcall) { 101167375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers visitor->VisitFrame(frame, pc); 101267375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers } 101355df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao } 101455df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao} 101555df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 101601158d7a57c8321370667a6045220237d16e0da8Elliott Hughesjobject Thread::CreateInternalStackTrace(JNIEnv* env) const { 1017aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Compute depth of stack 10189b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao CountStackDepthVisitor count_visitor; 10199b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao WalkStack(&count_visitor); 10209b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao int32_t depth = count_visitor.GetDepth(); 102129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int32_t skip_depth = count_visitor.GetSkipDepth(); 10224417536522fd2a9d8215d8672331984769c9520bShih-wei Liao 1023aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Transition into runnable state to work on Object*/Array* 102401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes ScopedJniThreadState ts(env); 1025aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 1026aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Build internal stack trace 102729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes BuildInternalStackTraceVisitor build_trace_visitor(depth, skip_depth, ts); 10289b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao WalkStack(&build_trace_visitor); 10294417536522fd2a9d8215d8672331984769c9520bShih-wei Liao 1030aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return build_trace_visitor.GetInternalStackTrace(); 1031aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers} 1032aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 103301158d7a57c8321370667a6045220237d16e0da8Elliott HughesjobjectArray Thread::InternalStackTraceToStackTraceElementArray(JNIEnv* env, jobject internal, 103401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobjectArray output_array, int* stack_depth) { 1035aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Transition into runnable state to work on Object*/Array* 1036aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ScopedJniThreadState ts(env); 1037aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 1038aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Decode the internal stack trace into the depth, method trace and PC trace 1039aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ObjectArray<Object>* method_trace = 1040aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); 1041aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers int32_t depth = method_trace->GetLength()-1; 1042aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); 1043aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 1044aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1045aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 104601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobjectArray result; 104701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes ObjectArray<StackTraceElement>* java_traces; 104801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes if (output_array != NULL) { 104901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Reuse the array we were given. 105001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes result = output_array; 105101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes java_traces = reinterpret_cast<ObjectArray<StackTraceElement>*>(Decode<Array*>(env, 105201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes output_array)); 105301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // ...adjusting the number of frames we'll write to not exceed the array length. 105401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes depth = std::min(depth, java_traces->GetLength()); 105501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } else { 105601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Create java_trace array and place in local reference table 105701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes java_traces = class_linker->AllocStackTraceElementArray(depth); 105801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes result = AddLocalReference<jobjectArray>(ts.Env(), java_traces); 105901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } 106001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 106101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes if (stack_depth != NULL) { 106201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes *stack_depth = depth; 106301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } 106455df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 10659b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao for (int32_t i = 0; i < depth; ++i) { 1066aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Prepare parameters for StackTraceElement(String cls, String method, String file, int line) 1067aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers Method* method = down_cast<Method*>(method_trace->Get(i)); 1068aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t native_pc = pc_trace->Get(i); 1069aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers Class* klass = method->GetDeclaringClass(); 107055df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao const DexFile& dex_file = class_linker->FindDexFile(klass->GetDexCache()); 107138933579b15c74baa09b8713ee8bf715529d05ebElliott Hughes std::string class_name(PrettyDescriptor(klass->GetDescriptor())); 107255df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 1073aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Allocate element, potentially triggering GC 107455df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao StackTraceElement* obj = 107538933579b15c74baa09b8713ee8bf715529d05ebElliott Hughes StackTraceElement::Alloc(String::AllocFromModifiedUtf8(class_name.c_str()), 10764417536522fd2a9d8215d8672331984769c9520bShih-wei Liao method->GetName(), 10774b620ffb1b4d0c96a94bb3afe314f35d53990ec6Brian Carlstrom klass->GetSourceFile(), 10784417536522fd2a9d8215d8672331984769c9520bShih-wei Liao dex_file.GetLineNumFromPC(method, 1079aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method->ToDexPC(native_pc))); 1080aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#ifdef MOVING_GARBAGE_COLLECTOR 1081aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Re-read after potential GC 1082aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers java_traces = Decode<ObjectArray<Object>*>(ts.Env(), result); 1083aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace = down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); 1084aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); 1085aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#endif 108655df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao java_traces->Set(i, obj); 108755df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao } 1088aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return result; 108955df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao} 109055df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 10915cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughesvoid Thread::ThrowNewExceptionF(const char* exception_class_descriptor, const char* fmt, ...) { 1092a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_list args; 1093a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_start(args, fmt); 10944a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes ThrowNewExceptionV(exception_class_descriptor, fmt, args); 1095a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_end(args); 10964a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes} 10974a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes 10984a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughesvoid Thread::ThrowNewExceptionV(const char* exception_class_descriptor, const char* fmt, va_list ap) { 10994a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes std::string msg; 11004a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes StringAppendV(&msg, fmt, ap); 11015cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes ThrowNewException(exception_class_descriptor, msg.c_str()); 11025cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes} 110337f7a40f6789bb287f287a9af00777af9d6428eeElliott Hughes 11045cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughesvoid Thread::ThrowNewException(const char* exception_class_descriptor, const char* msg) { 1105e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes // Convert "Ljava/lang/Exception;" into JNI-style "java/lang/Exception". 11060cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers CHECK_EQ('L', exception_class_descriptor[0]); 1107e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes std::string descriptor(exception_class_descriptor + 1); 11080cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers CHECK_EQ(';', descriptor[descriptor.length() - 1]); 1109e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes descriptor.erase(descriptor.length() - 1); 1110e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes 1111e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes JNIEnv* env = GetJniEnv(); 1112e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes jclass exception_class = env->FindClass(descriptor.c_str()); 1113e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes CHECK(exception_class != NULL) << "descriptor=\"" << descriptor << "\""; 11145cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes int rc = env->ThrowNew(exception_class, msg); 1115e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes CHECK_EQ(rc, JNI_OK); 1116bc2f3e3e41d02eb2896dc16390c5c4023a7b5649Brian Carlstrom env->DeleteLocalRef(exception_class); 1117a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes} 1118a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes 1119418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughesvoid Thread::ThrowOutOfMemoryError(Class* c, size_t byte_count) { 1120418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes if (!throwing_OOME_) { 1121418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes throwing_OOME_ = true; 1122418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes ThrowNewException("Ljava/lang/OutOfMemoryError;", NULL); 1123418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes LOG(ERROR) << "Failed to allocate a " << PrettyDescriptor(c->GetDescriptor()) << " (" << byte_count << " bytes)"; 1124418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes } else { 1125418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes UNIMPLEMENTED(FATAL) << "throw one i prepared earlier..."; 1126418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes } 1127418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes throwing_OOME_ = false; 112879082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes} 112979082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes 1130bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogersclass CatchBlockStackVisitor : public Thread::StackVisitor { 1131bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers public: 1132bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers CatchBlockStackVisitor(Class* to_find, Context* ljc) 113367375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers : found_(false), to_find_(to_find), long_jump_context_(ljc), native_method_count_(0) { 113467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers#ifndef NDEBUG 113567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_pc_ = 0xEBADC0DE; 113667375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_frame_.SetSP(reinterpret_cast<Method**>(0xEBADF00D)); 113767375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers#endif 113867375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers } 1139bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1140bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers virtual void VisitFrame(const Frame& fr, uintptr_t pc) { 1141bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (!found_) { 1142bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Method* method = fr.GetMethod(); 114367375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers if (method == NULL) { 114467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // This is the upcall, we remember the frame and last_pc so that we may 114567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // long jump to them 1146a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes handler_pc_ = DemanglePc(pc); 114767375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_frame_ = fr; 114867375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers return; 114967375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers } 115067375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uint32_t dex_pc = DexFile::kDexNoIndex; 11513320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom if (method->IsCalleeSaveMethod()) { 11529086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // ignore callee save method 11539086572fa809d1a19d886b467e4da3ce42016982Ian Rogers } else if (method->IsNative()) { 11549086572fa809d1a19d886b467e4da3ce42016982Ian Rogers native_method_count_++; 11559086572fa809d1a19d886b467e4da3ce42016982Ian Rogers } else { 11569086572fa809d1a19d886b467e4da3ce42016982Ian Rogers dex_pc = method->ToDexPC(pc); 1157bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1158bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (dex_pc != DexFile::kDexNoIndex) { 1159bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers uint32_t found_dex_pc = method->FindCatchBlock(to_find_, dex_pc); 1160bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (found_dex_pc != DexFile::kDexNoIndex) { 1161bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers found_ = true; 116267375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_pc_ = method->ToNativePC(found_dex_pc); 116367375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_frame_ = fr; 1164bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1165bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1166bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (!found_) { 1167bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Caller may be handler, fill in callee saves in context 1168bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers long_jump_context_->FillCalleeSaves(fr); 1169bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 11701a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 11711a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 11721a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 1173bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Did we find a catch block yet? 1174bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers bool found_; 1175bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // The type of the exception catch block to find 1176bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Class* to_find_; 1177bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Frame with found handler or last frame if no handler found 1178bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Frame handler_frame_; 117967375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // PC to branch to for the handler 118067375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uintptr_t handler_pc_; 1181bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Context that will be the target of the long jump 1182bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Context* long_jump_context_; 118367375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // Number of native methods passed in crawl (equates to number of SIRTs to pop) 118467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uint32_t native_method_count_; 1185bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers}; 1186bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1187ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogersvoid Thread::DeliverException() { 1188ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers Throwable *exception = GetException(); // Set exception on thread 1189ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers CHECK(exception != NULL); 1190bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1191bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Context* long_jump_context = GetLongJumpContext(); 1192bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers CatchBlockStackVisitor catch_finder(exception->GetClass(), long_jump_context); 119367375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers WalkStackUntilUpCall(&catch_finder, true); 1194bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 119567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // Pop any SIRT 119667375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers if (catch_finder.native_method_count_ == 1) { 119767375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers PopSirt(); 11981a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } else { 1199ad42e1327fca837531e4feac8841de3960c4df45Ian Rogers // We only expect the stack crawl to have passed 1 native method as it's terminated 1200ad42e1327fca837531e4feac8841de3960c4df45Ian Rogers // by an up call 120167375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers DCHECK_EQ(catch_finder.native_method_count_, 0u); 1202bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 120367375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers long_jump_context->SetSP(reinterpret_cast<intptr_t>(catch_finder.handler_frame_.GetSP())); 120467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers long_jump_context->SetPC(catch_finder.handler_pc_); 1205bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers long_jump_context->DoLongJump(); 1206bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers} 1207bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1208bdb0391258abc54bf77c676e36847d28a783bfe5Ian RogersContext* Thread::GetLongJumpContext() { 120985d1545e985ac689db4bad7849880e843707c862Elliott Hughes Context* result = long_jump_context_; 1210bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (result == NULL) { 1211bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers result = Context::Create(); 121285d1545e985ac689db4bad7849880e843707c862Elliott Hughes long_jump_context_ = result; 12131a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 1214bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers return result; 12151a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao} 12161a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 12175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesbool Thread::HoldsLock(Object* object) { 12185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (object == NULL) { 12195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 12205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 12215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return object->GetLockOwner() == thin_lock_id_; 12225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 12235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1224038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesbool Thread::IsDaemon() { 1225038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes return gThread_daemon->GetBoolean(peer_); 1226038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes} 1227038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 1228d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogersclass ReferenceMapVisitor : public Thread::StackVisitor { 1229d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers public: 1230d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers ReferenceMapVisitor(Context* context, Heap::RootVisitor* root_visitor, void* arg) : 1231d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers context_(context), root_visitor_(root_visitor), arg_(arg) { 1232d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1233d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1234d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers void VisitFrame(const Frame& frame, uintptr_t pc) { 1235d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Method* m = frame.GetMethod(); 1236d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Process register map (which native and callee save methods don't have) 12373320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom if (!m->IsNative() && !m->IsCalleeSaveMethod()) { 1238d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers UniquePtr<art::DexVerifier::RegisterMap> map(art::DexVerifier::GetExpandedRegisterMap(m)); 1239f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers const uint8_t* reg_bitmap = art::DexVerifier::RegisterMapGetLine(map.get(), m->ToDexPC(pc)); 12404f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao LOG(INFO) << "Visiting stack roots in " << PrettyMethod(m, false) 1241f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers << "@ PC: " << m->ToDexPC(pc); 1242d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers CHECK(reg_bitmap != NULL); 12433320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom const uint16_t* vmap = m->GetVmapTable(); 1244d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // For all dex registers 1245d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers for (int reg = 0; reg < m->NumRegisters(); ++reg) { 1246d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Does this register hold a reference? 1247d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers if (TestBitmap(reg, reg_bitmap)) { 1248d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Is the reference in the context or on the stack? 1249d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers bool in_context = false; 1250f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers uint32_t vmap_offset = 0xEBAD0FF5; 1251d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // TODO: take advantage of the registers being ordered 12523320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom for (int i = 0; i < m->GetVmapTableLength(); i++) { 12533320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom if (vmap[i] == reg) { 1254d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers in_context = true; 1255d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers vmap_offset = i; 1256d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers break; 1257d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1258d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1259d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Object* ref; 1260d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers if (in_context) { 1261d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Compute the register we need to load from the context 1262d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers uint32_t spill_mask = m->GetCoreSpillMask(); 1263f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers uint32_t matches = 0; 1264f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers uint32_t spill_shifts = 0; 1265f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers while (matches != (vmap_offset + 1)) { 1266f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers CHECK_NE(spill_mask, 0u); 1267f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers matches += spill_mask & 1; // Add 1 if the low bit is set 1268f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_mask >>= 1; 1269f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_shifts++; 1270d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1271f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_shifts--; // wind back one as we want the last match 1272f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers ref = reinterpret_cast<Object*>(context_->GetGPR(spill_shifts)); 1273d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } else { 1274d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers ref = reinterpret_cast<Object*>(frame.GetVReg(m ,reg)); 1275d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 12764f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao if (ref != NULL) { 12774f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao root_visitor_(ref, arg_); 12784f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao } 1279d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1280d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1281d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1282d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers context_->FillCalleeSaves(frame); 1283d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1284d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1285d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers private: 1286d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers bool TestBitmap(int reg, const uint8_t* reg_vector) { 1287d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers return ((reg_vector[reg / 8] >> (reg % 8)) & 0x01) != 0; 1288d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1289d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1290d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Context used to build up picture of callee saves 1291d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Context* context_; 1292d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Call-back when we visit a root 1293d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Heap::RootVisitor* root_visitor_; 1294d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Argument to call-back 1295d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers void* arg_; 1296d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers}; 1297d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1298d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogersvoid Thread::VisitRoots(Heap::RootVisitor* visitor, void* arg) { 1299d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (exception_ != NULL) { 1300d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes visitor(exception_, arg); 1301d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 1302d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (peer_ != NULL) { 1303d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes visitor(peer_, arg); 1304d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 1305410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes jni_env_->locals.VisitRoots(visitor, arg); 1306410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes jni_env_->monitors.VisitRoots(visitor, arg); 13078dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 13088dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao SirtVisitRoots(visitor, arg); 13098dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 1310d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Cheat and steal the long jump context. Assume that we are not doing a GC during exception 1311d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // delivery. 1312d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Context* context = GetLongJumpContext(); 1313d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Visit roots on this thread's stack 1314d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers ReferenceMapVisitor mapper(context, visitor, arg); 1315d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers WalkStack(&mapper); 1316410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes} 1317410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes 1318b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogersstatic const char* kStateNames[] = { 131993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "Terminated", 1320b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers "Runnable", 132193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "TimedWaiting", 1322b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers "Blocked", 1323b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers "Waiting", 132493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "Initializing", 132593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "Starting", 1326b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers "Native", 132793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "VmWait", 132893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes "Suspended", 1329b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers}; 1330b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogersstd::ostream& operator<<(std::ostream& os, const Thread::State& state) { 13318e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes int32_t int_state = static_cast<int32_t>(state); 133293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes if (state >= Thread::kTerminated && state <= Thread::kSuspended) { 133393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes os << kStateNames[int_state]; 1334b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers } else { 133593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes os << "State[" << int_state << "]"; 1336b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers } 1337b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers return os; 1338b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers} 1339b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers 1340330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughesstd::ostream& operator<<(std::ostream& os, const Thread& thread) { 1341330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes os << "Thread[" << &thread 1342e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes << ",pthread_t=" << thread.GetImpl() 1343e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes << ",tid=" << thread.GetTid() 1344dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes << ",id=" << thread.GetThinLockId() 13458daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes << ",state=" << thread.GetState() 13468daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes << ",peer=" << thread.GetPeer() 13478daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes << "]"; 1348330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes return os; 1349330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes} 1350330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes 13518daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} // namespace art 1352