thread.cc revision 3ea0f42467790809fcfc9fc861605d465808090f
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> 22dbf05b722af99ba2fd2f4c4fc7eb6c3e9880e5d1Brian Carlstrom#include <sys/resource.h> 23dbf05b722af99ba2fd2f4c4fc7eb6c3e9880e5d1Brian Carlstrom#include <sys/time.h> 24a09576416788b916095739e43a16917e7948f3a4Elliott Hughes 25b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro#include <algorithm> 26dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes#include <bitset> 27eb4f614f2eb53b92ebd416fa418f550861655887Elliott Hughes#include <cerrno> 28a09576416788b916095739e43a16917e7948f3a4Elliott Hughes#include <iostream> 29b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro#include <list> 30b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 31872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes#include "debugger.h" 32a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes#include "class_linker.h" 33df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom#include "class_loader.h" 34408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers#include "heap.h" 35c5f7c91ab89055cffb573fff7e06dbdd860bccedElliott Hughes#include "jni_internal.h" 368e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes#include "monitor.h" 37776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 3857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers#include "oat/runtime/context.h" 39776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 40a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes#include "object.h" 416d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h" 429a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson#include "reflection.h" 43578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "runtime.h" 445433072f589b61413e042eddf76e8190a048f71dbuzbee#include "runtime_support.h" 45726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes#include "ScopedLocalRef.h" 46aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#include "scoped_jni_thread_state.h" 47f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien#include "shadow_frame.h" 4830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers#include "space.h" 4968e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack.h" 5068e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack_indirect_reference_table.h" 518daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "thread_list.h" 52a09576416788b916095739e43a16917e7948f3a4Elliott Hughes#include "utils.h" 53776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#include "verifier/gc_map.h" 54b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 55b557353b22c728eecbd1c68593b482622c7782a8Carl Shapironamespace art { 56b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 57b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiropthread_key_t Thread::pthread_key_self_; 58b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 59462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughesstatic Class* gThreadGroup = NULL; 608e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesstatic Class* gThreadLock = NULL; 61038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_daemon = NULL; 62038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_group = NULL; 63038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_lock = NULL; 64038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_name = NULL; 65038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_priority = NULL; 6629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Field* gThread_uncaughtHandler = NULL; 67038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_vmData = NULL; 68462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughesstatic Field* gThreadGroup_mMain = NULL; 69462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughesstatic Field* gThreadGroup_mSystem = NULL; 70038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThreadGroup_name = NULL; 718e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesstatic Field* gThreadLock_thread = NULL; 72038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Method* gThread_run = NULL; 7329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Method* gThreadGroup_removeThread = NULL; 7429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Method* gUncaughtExceptionHandler_uncaughtException = NULL; 75038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 765d76c435082332ef79a22962386fa92a0870e378Ian Rogersvoid Thread::InitCardTable() { 77b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes card_table_ = Runtime::Current()->GetHeap()->GetCardTable()->GetBiasedBegin(); 785d76c435082332ef79a22962386fa92a0870e378Ian Rogers} 795d76c435082332ef79a22962386fa92a0870e378Ian Rogers 803ea0f42467790809fcfc9fc861605d465808090fElliott Hughesstatic void UnimplementedEntryPoint() { 813ea0f42467790809fcfc9fc861605d465808090fElliott Hughes UNIMPLEMENTED(FATAL); 823ea0f42467790809fcfc9fc861605d465808090fElliott Hughes} 833ea0f42467790809fcfc9fc861605d465808090fElliott Hughes 843ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbeevoid Thread::InitFunctionPointers() { 85776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if defined(ART_USE_LLVM_COMPILER) 86776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers memset(&entrypoints_, 0, sizeof(entrypoints_)); 87776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#else 883ea0f42467790809fcfc9fc861605d465808090fElliott Hughes // Insert a placeholder so we can easily tell if we call an unimplemented entry point. 893ea0f42467790809fcfc9fc861605d465808090fElliott Hughes uintptr_t* begin = reinterpret_cast<uintptr_t*>(&entrypoints_); 903ea0f42467790809fcfc9fc861605d465808090fElliott Hughes uintptr_t* end = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(begin) + sizeof(entrypoints_)); 913ea0f42467790809fcfc9fc861605d465808090fElliott Hughes for (uintptr_t* it = begin; it != end; ++it) { 923ea0f42467790809fcfc9fc861605d465808090fElliott Hughes *it = reinterpret_cast<uintptr_t>(UnimplementedEntryPoint); 933ea0f42467790809fcfc9fc861605d465808090fElliott Hughes } 9457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers InitEntryPoints(&entrypoints_); 95776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 96c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes} 97c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes 98c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesvoid Thread::SetDebuggerUpdatesEnabled(bool enabled) { 99c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes LOG(INFO) << "Turning debugger updates " << (enabled ? "on" : "off") << " for " << *this; 100776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 10157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers ChangeDebuggerEntryPoint(&entrypoints_, enabled); 102776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#else 103776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers UNIMPLEMENTED(FATAL); 104776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 1053ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee} 1063ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee 107caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstromvoid Thread::InitTid() { 108caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom tid_ = ::art::GetTid(); 109caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom} 110caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom 111caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstromvoid Thread::InitAfterFork() { 112caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom InitTid(); 1133147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes#if defined(__BIONIC__) 1143147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes // Work around a bionic bug. 1153147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes struct bionic_pthread_internal_t { 1163147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes void* next; 1173147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes void** pref; 1183147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes pthread_attr_t attr; 1193147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes pid_t kernel_id; 1203147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes // et cetera. we just need 'kernel_id' so we can stop here. 1213147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes }; 1223147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes reinterpret_cast<bionic_pthread_internal_t*>(pthread_self())->kernel_id = tid_; 1233147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes#endif 124caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom} 125caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom 12678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromvoid* Thread::CreateCallback(void* arg) { 12793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* self = reinterpret_cast<Thread*>(arg); 128462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes self->Init(); 12993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 13093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Wait until it's safe to start running code. (There may have been a suspend-all 13193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // in progress while we were starting up.) 132462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes Runtime* runtime = Runtime::Current(); 13393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes runtime->GetThreadList()->WaitForGo(); 13493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 13547179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes { 13634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes CHECK_EQ(self->GetState(), kRunnable); 137899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes SirtRef<String> thread_name(self->GetThreadName()); 138899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes self->SetThreadName(thread_name->ToModifiedUtf8().c_str()); 13947179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes } 14047179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes 141872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes Dbg::PostThreadStart(self); 14293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 14393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Invoke the 'run' method of our java.lang.Thread. 14493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes CHECK(self->peer_ != NULL); 14593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Object* receiver = self->peer_; 146038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes Method* m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(gThread_run); 14793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes m->Invoke(self, receiver, NULL, NULL); 14893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 14993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Detach. 15093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes runtime->GetThreadList()->Unregister(); 15193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 152b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro return NULL; 153b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 154b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 155ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic void SetVmData(Object* managed_thread, Thread* native_thread) { 156038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes gThread_vmData->SetInt(managed_thread, reinterpret_cast<uintptr_t>(native_thread)); 15793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 15893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 159761928d24e4e7ed7776b52243eaf9095ad35f448Elliott HughesThread* Thread::FromManagedThread(Object* thread_peer) { 160761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes return reinterpret_cast<Thread*>(static_cast<uintptr_t>(gThread_vmData->GetInt(thread_peer))); 161761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes} 162761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes 16301158d7a57c8321370667a6045220237d16e0da8Elliott HughesThread* Thread::FromManagedThread(JNIEnv* env, jobject java_thread) { 164761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes return FromManagedThread(Decode<Object*>(env, java_thread)); 16501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes} 16601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 167ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic size_t FixStackSize(size_t stack_size) { 1687502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // A stack size of zero means "use the default". 169d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (stack_size == 0) { 170d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes stack_size = Runtime::Current()->GetDefaultStackSize(); 171d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 17261e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 1736414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom // Dalvik used the bionic pthread default stack size for native threads, 1746414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom // so include that here to support apps that expect large native stacks. 1756414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom stack_size += 1 * MB; 1766414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom 1776414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom 1787502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // It's not possible to request a stack smaller than the system-defined PTHREAD_STACK_MIN. 1797502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes if (stack_size < PTHREAD_STACK_MIN) { 1807502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = PTHREAD_STACK_MIN; 1817502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes } 1827502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 1837502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // It's likely that callers are trying to ensure they have at least a certain amount of 1847502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // stack space, so we should add our reserved space on top of what they requested, rather 1857502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // than implicitly take it away from them. 1867502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size += Thread::kStackOverflowReservedBytes; 1877502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 1887502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // Some systems require the stack size to be a multiple of the system page size, so round up. 1897502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = RoundUp(stack_size, kPageSize); 1907502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 1917502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes return stack_size; 1927502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes} 1937502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 1947502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughesvoid Thread::Create(Object* peer, size_t stack_size) { 1957502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes CHECK(peer != NULL); 1967502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 1977502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = FixStackSize(stack_size); 1987502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 19993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* native_thread = new Thread; 20093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes native_thread->peer_ = peer; 20193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 20293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Thread.start is synchronized, so we know that vmData is 0, 20393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // and know that we're not racing to assign it. 20493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes SetVmData(peer, native_thread); 20561e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 20647179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes { 20734e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes ScopedThreadStateChange tsc(Thread::Current(), kVmWait); 20847179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes pthread_t new_pthread; 20947179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes pthread_attr_t attr; 21047179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread"); 21147179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), "PTHREAD_CREATE_DETACHED"); 21247179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size); 21347179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_create, (&new_pthread, &attr, Thread::CreateCallback, native_thread), "new thread"); 21447179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread"); 21547179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes } 21661e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 21793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Let the child know when it's safe to start running. 21893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Runtime::Current()->GetThreadList()->SignalGo(native_thread); 21993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 22061e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 221462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughesvoid Thread::Init() { 222462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // This function does all the initialization that must be run by the native thread it applies to. 223462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // (When we create a new thread from managed code, we allocate the Thread* in Thread::Create so 224462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // we can handshake with the corresponding native thread when it's ready.) Check this native 225462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // thread hasn't been through here already... 226cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes CHECK(Thread::Current() == NULL); 227cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes 22893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitCpu(); 22993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitFunctionPointers(); 2305d76c435082332ef79a22962386fa92a0870e378Ian Rogers InitCardTable(); 23161e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 232462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes Runtime* runtime = Runtime::Current(); 233462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes CHECK(runtime != NULL); 234462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes 235462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes thin_lock_id_ = runtime->GetThreadList()->AllocThreadId(); 236be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 237caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom InitTid(); 23893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitStackHwm(); 239dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 2403147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach"); 241a5780dad67556297c8ca5f2608c53b193e6c4514Elliott Hughes 24293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes jni_env_ = new JNIEnvExt(this, runtime->GetJavaVM()); 2435fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 2447a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes runtime->GetThreadList()->Register(); 24593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 24693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 247462c94449720e0dc6b93d7138d835d345ccf67aeElliott HughesThread* Thread::Attach(const char* thread_name, bool as_daemon, Object* thread_group) { 248cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes Thread* self = new Thread; 249462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes self->Init(); 2505fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 25134e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes self->SetState(kNative); 252726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes 253cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes // If we're the main thread, ClassLinker won't be created until after we're attached, 254cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes // so that thread needs a two-stage attach. Regular threads don't need this hack. 255d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes // In the compiler, all threads need this hack, because no-one's going to be getting 256d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes // a native peer! 257d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes if (self->thin_lock_id_ != ThreadList::kMainId && !Runtime::Current()->IsCompiler()) { 258462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes self->CreatePeer(thread_name, as_daemon, thread_group); 25906e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes } else { 26006e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes // These aren't necessary, but they improve diagnostics for unit tests & command-line tools. 26122869a9026a08b544eca4cefd67386d347e30d2cElliott Hughes if (thread_name != NULL) { 26222869a9026a08b544eca4cefd67386d347e30d2cElliott Hughes self->name_->assign(thread_name); 26322869a9026a08b544eca4cefd67386d347e30d2cElliott Hughes ::art::SetThreadName(thread_name); 26422869a9026a08b544eca4cefd67386d347e30d2cElliott Hughes } 26536e0a955e48e9357bff1eab783e493107ebfff62Ian Rogers } 266cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes 267cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes self->GetJniEnv()->locals.AssertEmpty(); 2685fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes return self; 2695fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes} 2705fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 271462c94449720e0dc6b93d7138d835d345ccf67aeElliott HughesObject* Thread::GetMainThreadGroup() { 2720045a290e6b79a274250e3112880c04bde437d4aIan Rogers if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(gThreadGroup, true, true)) { 273462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes return NULL; 274462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes } 275462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes return gThreadGroup_mMain->GetObject(NULL); 276d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes} 277d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 278462c94449720e0dc6b93d7138d835d345ccf67aeElliott HughesObject* Thread::GetSystemThreadGroup() { 2790045a290e6b79a274250e3112880c04bde437d4aIan Rogers if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(gThreadGroup, true, true)) { 280462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes return NULL; 281462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes } 282462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes return gThreadGroup_mSystem->GetObject(NULL); 283462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes} 284462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes 285462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughesvoid Thread::CreatePeer(const char* name, bool as_daemon, Object* thread_group) { 2869a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson CHECK(Runtime::Current()->IsStarted()); 2875fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes JNIEnv* env = jni_env_; 2885fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 289462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes if (thread_group == NULL) { 290462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes thread_group = Thread::GetMainThreadGroup(); 291462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes } 292462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes ScopedLocalRef<jobject> java_thread_group(env, AddLocalReference<jobject>(env, thread_group)); 293726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF(name)); 2948daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes jint thread_priority = GetNativePriority(); 2955fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes jboolean thread_is_daemon = as_daemon; 2965fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 297726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Thread")); 2985d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers ScopedLocalRef<jobject> peer(env, env->AllocObject(c.get())); 299726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes peer_ = DecodeJObject(peer.get()); 3005d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers if (peer_ == NULL) { 3015d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers CHECK(IsExceptionPending()); 3025d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers return; 3035d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers } 3045d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers jmethodID mid = env->GetMethodID(c.get(), "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V"); 305462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes env->CallNonvirtualVoidMethod(peer.get(), c.get(), mid, java_thread_group.get(), thread_name.get(), thread_priority, thread_is_daemon); 3069a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson CHECK(!IsExceptionPending()) << " " << PrettyTypeOf(GetException()); 3077a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes SetVmData(peer_, Thread::Current()); 308d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 309899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes SirtRef<String> peer_thread_name(GetThreadName()); 31000fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom if (peer_thread_name.get() == NULL) { 31100fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // The Thread constructor should have set the Thread.name to a 31200fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // non-null value. However, because we can run without code 31300fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // available (in the compiler, in tests), we manually assign the 31400fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // fields the constructor should have set. 31500fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom gThread_daemon->SetBoolean(peer_, thread_is_daemon); 316462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes gThread_group->SetObject(peer_, thread_group); 31700fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom gThread_name->SetObject(peer_, Decode<Object*>(env, thread_name.get())); 31800fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom gThread_priority->SetInt(peer_, thread_priority); 319899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes peer_thread_name.reset(GetThreadName()); 32000fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom } 32100fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // thread_name may have been null, so don't trust this to be non-null 32200fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom if (peer_thread_name.get() != NULL) { 323899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes SetThreadName(peer_thread_name->ToModifiedUtf8().c_str()); 32400fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom } 325726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes 326726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes // Pre-allocate an OutOfMemoryError for the double-OOME case. 327726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ThrowNewException("Ljava/lang/OutOfMemoryError;", 328726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes "OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available"); 329726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jthrowable> exception(env, env->ExceptionOccurred()); 330726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes env->ExceptionClear(); 331726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes pre_allocated_OutOfMemoryError_ = Decode<Throwable*>(env, exception.get()); 33261e019d291583029c01b61b93bea750f2b663c37Carl Shapiro} 33361e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 334899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughesvoid Thread::SetThreadName(const char* name) { 335899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes name_->assign(name); 336899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes ::art::SetThreadName(name); 337899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes Dbg::DdmSendThreadNotification(this, CHUNK_TYPE("THNM")); 338899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes} 339899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes 340be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::InitStackHwm() { 341215f3144be114ac27002059330d903bf1ff5a592Elliott Hughes#if defined(__APPLE__) 3426cc332ef80edf56221676e33ea16bb4022130344Elliott Hughes // Only needed to run code. Try implementing this with pthread_get_stacksize_np and pthread_get_stackaddr_np. 3436cc332ef80edf56221676e33ea16bb4022130344Elliott Hughes UNIMPLEMENTED(WARNING); 344215f3144be114ac27002059330d903bf1ff5a592Elliott Hughes#else 345be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes pthread_attr_t attributes; 3463147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes CHECK_PTHREAD_CALL(pthread_getattr_np, (pthread_self(), &attributes), __FUNCTION__); 347be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 348932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers void* temp_stack_base; 349932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, &temp_stack_base, &stack_size_), 350932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers __FUNCTION__); 35130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers stack_begin_ = reinterpret_cast<byte*>(temp_stack_base); 352be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 353932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers if (stack_size_ <= kStackOverflowReservedBytes) { 3543d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes LOG(FATAL) << "Attempt to attach a thread with a too-small stack (" << stack_size_ << " bytes)"; 355be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 356449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes 357932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // Set stack_end_ to the bottom of the stack saving space of stack overflows 358932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers ResetDefaultStackEnd(); 359449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes 360449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes // Sanity check. 361449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes int stack_variable; 362398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes CHECK_GT(&stack_variable, reinterpret_cast<void*>(stack_end_)); 363be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 3648d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); 365215f3144be114ac27002059330d903bf1ff5a592Elliott Hughes#endif 366be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes} 367be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 368899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughesvoid Thread::Dump(std::ostream& os, bool full) const { 369899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes if (full) { 370899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes DumpState(os); 371899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes DumpStack(os); 372899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes } else { 373899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes os << "Thread["; 374899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes if (GetThinLockId() != 0) { 375899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes // If we're in kStarting, we won't have a thin lock id or tid yet. 376899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes os << GetThinLockId() 377899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes << ",tid=" << GetTid() << ','; 378899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes } 379899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes os << GetState() 380899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes << ",Thread*=" << this 381ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes << ",peer=" << peer_ 382899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes << ",\"" << *name_ << "\"" 383899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes << "]"; 384e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes } 385d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes} 386d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 387899e789bd4741c0172268f7838ce8ab220a5f916Elliott HughesString* Thread::GetThreadName() const { 388fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes return (peer_ != NULL) ? reinterpret_cast<String*>(gThread_name->GetObject(peer_)) : NULL; 389fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes} 390fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 391ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughesvoid Thread::GetThreadName(std::string& name) const { 392ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes name.assign(*name_); 393ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes} 394ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes 395d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesvoid Thread::DumpState(std::ostream& os) const { 396d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::string group_name; 397d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes int priority; 398d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes bool is_daemon = false; 399d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 400d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (peer_ != NULL) { 401038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes priority = gThread_priority->GetInt(peer_); 402038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes is_daemon = gThread_daemon->GetBoolean(peer_); 403d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 404a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes Object* thread_group = GetThreadGroup(); 405d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (thread_group != NULL) { 406038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes String* group_name_string = reinterpret_cast<String*>(gThreadGroup_name->GetObject(thread_group)); 407d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes group_name = (group_name_string != NULL) ? group_name_string->ToModifiedUtf8() : "<null>"; 408d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 409d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } else { 410d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes priority = GetNativePriority(); 411dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes } 412d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 413d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int policy; 414d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes sched_param sp; 4153147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes CHECK_PTHREAD_CALL(pthread_getschedparam, (pthread_self(), &policy, &sp), __FUNCTION__); 416d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 4171bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes std::string scheduler_group_name(GetSchedulerGroupName(GetTid())); 4181bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes if (scheduler_group_name.empty()) { 4191bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes scheduler_group_name = "default"; 420d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 421d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 422899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes os << '"' << *name_ << '"'; 423d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (is_daemon) { 424d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " daemon"; 425d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 426d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " prio=" << priority 427dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes << " tid=" << GetThinLockId() 42893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes << " " << GetState() << "\n"; 429d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 430d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | group=\"" << group_name << "\"" 4318d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes << " sCount=" << suspend_count_ 432234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes << " dsCount=" << debug_suspend_count_ 433dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes << " obj=" << reinterpret_cast<void*>(peer_) 434d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " self=" << reinterpret_cast<const void*>(this) << "\n"; 435d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | sysTid=" << GetTid() 436d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " nice=" << getpriority(PRIO_PROCESS, GetTid()) 437d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " sched=" << policy << "/" << sp.sched_priority 4381bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes << " cgrp=" << scheduler_group_name 4393147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes << " handle=" << pthread_self() << "\n"; 440d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 441d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // Grab the scheduler stats for this thread. 442d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string scheduler_stats; 443d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", GetTid()).c_str(), &scheduler_stats)) { 444d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'. 445d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } else { 446d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_stats = "0 0 0"; 447d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 448d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 449d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int utime = 0; 450d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int stime = 0; 451d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int task_cpu = 0; 452bfe487be25652c5456236661b9d9c3579d2296c1Elliott Hughes GetTaskStats(GetTid(), utime, stime, task_cpu); 453d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 454d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | schedstat=( " << scheduler_stats << " )" 455d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " utm=" << utime 456d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " stm=" << stime 457d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " core=" << task_cpu 458d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " HZ=" << sysconf(_SC_CLK_TCK) << "\n"; 459a09576416788b916095739e43a16917e7948f3a4Elliott Hughes} 460a09576416788b916095739e43a16917e7948f3a4Elliott Hughes 461ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#if !defined(ART_USE_LLVM_COMPILER) 462b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogersvoid Thread::PushNativeToManagedRecord(NativeToManagedRecord* record) { 463b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers Method **sp = top_of_managed_stack_.GetSP(); 464b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers#ifndef NDEBUG 465b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers if (sp != NULL) { 466b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers Method* m = *sp; 467b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes Runtime::Current()->GetHeap()->VerifyObject(m); 468b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers DCHECK((m == NULL) || m->IsMethod()); 469b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers } 470b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers#endif 471b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers record->last_top_of_managed_stack_ = reinterpret_cast<void*>(sp); 472b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers record->last_top_of_managed_stack_pc_ = top_of_managed_stack_pc_; 473b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers record->link_ = native_to_managed_record_; 474b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers native_to_managed_record_ = record; 475b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers top_of_managed_stack_.SetSP(NULL); 476b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers} 477ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#else 478ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYavoid Thread::PushNativeToManagedRecord(NativeToManagedRecord*) { 479ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa LOG(FATAL) << "Called non-LLVM method with LLVM"; 480ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa} 481ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#endif 482b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers 483ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#if !defined(ART_USE_LLVM_COMPILER) 484b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogersvoid Thread::PopNativeToManagedRecord(const NativeToManagedRecord& record) { 485b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers native_to_managed_record_ = record.link_; 486b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers top_of_managed_stack_.SetSP(reinterpret_cast<Method**>(record.last_top_of_managed_stack_)); 487b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers top_of_managed_stack_pc_ = record.last_top_of_managed_stack_pc_; 488b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers} 489ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#else 490ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYavoid Thread::PopNativeToManagedRecord(const NativeToManagedRecord&) { 491ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa LOG(FATAL) << "Called non-LLVM method with LLVM"; 492ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa} 493ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#endif 494b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers 495d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughesstruct StackDumpVisitor : public Thread::StackVisitor { 4968e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes StackDumpVisitor(std::ostream& os, const Thread* thread) 49728ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers : last_method(NULL), last_line_number(0), repetition_count(0), os(os), thread(thread), 49828ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers frame_count(0) { 499d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 500d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 501bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers virtual ~StackDumpVisitor() { 502d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 503d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 504530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes bool VisitFrame(const Frame& frame, uintptr_t pc) { 5059086572fa809d1a19d886b467e4da3ce42016982Ian Rogers if (!frame.HasMethod()) { 506530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 5079086572fa809d1a19d886b467e4da3ce42016982Ian Rogers } 50828ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers const int kMaxRepetition = 3; 509d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Method* m = frame.GetMethod(); 510540a5b768bf5f76bd71ebf66612fb2298899480fTDYa#if !defined(ART_USE_LLVM_COMPILER) 511d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Class* c = m->GetDeclaringClass(); 5128e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 513b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers const DexCache* dex_cache = c->GetDexCache(); 514b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers int line_number = -1; 515b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers if (dex_cache != NULL) { // be tolerant of bad input 516b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers const DexFile& dex_file = class_linker->FindDexFile(dex_cache); 517b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers line_number = dex_file.GetLineNumFromPC(m, m->ToDexPC(pc)); 518b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers } 519540a5b768bf5f76bd71ebf66612fb2298899480fTDYa#else 520540a5b768bf5f76bd71ebf66612fb2298899480fTDYa // Compiler_LLVM stores line_number in the ShadowFrame, and passes it to visitor. 521540a5b768bf5f76bd71ebf66612fb2298899480fTDYa int line_number = static_cast<int>(pc); 522540a5b768bf5f76bd71ebf66612fb2298899480fTDYa#endif 52328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (line_number == last_line_number && last_method == m) { 52428ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers repetition_count++; 525d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } else { 52628ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (repetition_count >= kMaxRepetition) { 52728ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << " ... repeated " << (repetition_count - kMaxRepetition) << " times\n"; 52828ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 52928ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers repetition_count = 0; 53028ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers last_line_number = line_number; 53128ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers last_method = m; 53228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 53328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (repetition_count < kMaxRepetition) { 53428ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << " at " << PrettyMethod(m, false); 53528ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (m->IsNative()) { 53628ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << "(Native method)"; 53728ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } else { 5386d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers mh.ChangeMethod(m); 5396d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers const char* source_file(mh.GetDeclaringClassSourceFile()); 5406d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers os << "(" << (source_file != NULL ? source_file : "unavailable") 54128ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers << ":" << line_number << ")"; 54228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 54328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << "\n"; 544d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 5458e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 5468e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (frame_count++ == 0) { 5478e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Monitor::DescribeWait(os, thread); 5488e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 549530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 550d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 5516d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers MethodHelper mh; 55228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers Method* last_method; 55328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers int last_line_number; 55428ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers int repetition_count; 555d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::ostream& os; 5568e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes const Thread* thread; 5578e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes int frame_count; 558d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes}; 559d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 560d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesvoid Thread::DumpStack(std::ostream& os) const { 561ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // If we're currently in native code, dump that stack before dumping the managed stack. 56234e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes if (GetState() == kNative || GetState() == kVmWait) { 563ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes DumpNativeStack(os); 564ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 5658e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes StackDumpVisitor dumper(os, this); 566d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes WalkStack(&dumper); 567e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes} 568e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes 56934e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughesvoid Thread::SetStateWithoutSuspendCheck(ThreadState new_state) { 570a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes volatile void* raw = reinterpret_cast<volatile void*>(&state_); 571a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes volatile int32_t* addr = reinterpret_cast<volatile int32_t*>(raw); 572a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes android_atomic_release_store(new_state, addr); 573a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes} 574a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes 57534e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott HughesThreadState Thread::SetState(ThreadState new_state) { 57634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes ThreadState old_state = state_; 5778d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (old_state == new_state) { 5788d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes return old_state; 5798d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 5808d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 5818d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes volatile void* raw = reinterpret_cast<volatile void*>(&state_); 5828d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes volatile int32_t* addr = reinterpret_cast<volatile int32_t*>(raw); 5838d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 58434e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes if (new_state == kRunnable) { 5858d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes /* 58634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes * Change our status to kRunnable. The transition requires 58781ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes * that we check for pending suspension, because the runtime considers 5888d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * us to be "asleep" in all other states, and another thread could 5898d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * be performing a GC now. 5908d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5918d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * The order of operations is very significant here. One way to 5928d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * do this wrong is: 5938d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 5948d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * GCing thread Our thread (in kNative) 5958d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * ------------ ---------------------- 5968d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * check suspend count (== 0) 5978d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * SuspendAllThreads() 5988d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * grab suspend-count lock 5998d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * increment all suspend counts 6008d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * release suspend-count lock 6018d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * check thread state (== kNative) 6028d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * all are suspended, begin GC 6038d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * set state to kRunnable 6048d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * (continue executing) 6058d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6068d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We can correct this by grabbing the suspend-count lock and 6078d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * performing both of our operations (check suspend count, set 6088d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * state) while holding it, now we need to grab a mutex on every 6098d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * transition to kRunnable. 6108d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6118d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * What we do instead is change the order of operations so that 6128d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * the transition to kRunnable happens first. If we then detect 6138d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * that the suspend count is nonzero, we switch to kSuspended. 6148d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6158d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Appropriate compiler and memory barriers are required to ensure 6168d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * that the operations are observed in the expected order. 6178d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6188d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * This does create a small window of opportunity where a GC in 6198d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * progress could observe what appears to be a running thread (if 6208d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * it happens to look between when we set to kRunnable and when we 6218d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * switch to kSuspended). At worst this only affects assertions 6228d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * and thread logging. (We could work around it with some sort 6238d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * of intermediate "pre-running" state that is generally treated 6248d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * as equivalent to running, but that doesn't seem worthwhile.) 6258d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6268d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We can also solve this by combining the "status" and "suspend 6278d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * count" fields into a single 32-bit value. This trades the 6288d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * store/load barrier on transition to kRunnable for an atomic RMW 6298d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * op on all transitions and all suspend count updates (also, all 6308d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * accesses to status or the thread count require bit-fiddling). 6318d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * It also eliminates the brief transition through kRunnable when 6328d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * the thread is supposed to be suspended. This is possibly faster 6338d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * on SMP and slightly more correct, but less convenient. 6348d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes */ 6358d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes android_atomic_acquire_store(new_state, addr); 63606e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes ANNOTATE_IGNORE_READS_BEGIN(); 63706e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes int suspend_count = suspend_count_; 63806e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes ANNOTATE_IGNORE_READS_END(); 63906e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes if (suspend_count != 0) { 6408d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes Runtime::Current()->GetThreadList()->FullSuspendCheck(this); 6418d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 6428d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } else { 6438d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes /* 64434e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes * Not changing to kRunnable. No additional work required. 6458d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6468d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We use a releasing store to ensure that, if we were runnable, 6478d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * any updates we previously made to objects on the managed heap 6488d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * will be observed before the state change. 6498d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes */ 6508d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes android_atomic_release_store(new_state, addr); 6518d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 6528d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 6538d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes return old_state; 6548d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes} 6558d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 656761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughesbool Thread::IsSuspended() { 65706e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes ANNOTATE_IGNORE_READS_BEGIN(); 65806e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes int suspend_count = suspend_count_; 65906e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes ANNOTATE_IGNORE_READS_END(); 66034e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes return suspend_count != 0 && GetState() != kRunnable; 661761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes} 662761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes 663ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughesstatic void ReportThreadSuspendTimeout(Thread* waiting_thread) { 664ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes Runtime* runtime = Runtime::Current(); 665ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes std::ostringstream ss; 666ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes ss << "Thread suspend timeout waiting for thread " << *waiting_thread << "\n"; 667ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes runtime->DumpLockHolders(ss); 668ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes ss << "\n"; 669ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes runtime->GetThreadList()->DumpLocked(ss); 670ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes LOG(FATAL) << ss.str(); 671ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes} 672ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes 6738d768a954b101a9532f980253ac46be2c53aba11Elliott Hughesvoid Thread::WaitUntilSuspended() { 67421a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes static const useconds_t kTimeoutUs = 30 * 1000000; // 30s. 67521a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes 67621a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes useconds_t total_delay = 0; 6778d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes useconds_t delay = 0; 67834e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes while (GetState() == kRunnable) { 67921a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes if (total_delay >= kTimeoutUs) { 680ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes ReportThreadSuspendTimeout(this); 68121a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes } 6828d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes useconds_t new_delay = delay * 2; 6838d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_GE(new_delay, delay); 6848d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes delay = new_delay; 6858d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (delay == 0) { 6868d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes sched_yield(); 6878d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes delay = 10000; 6888d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } else { 6898d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes usleep(delay); 69021a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes total_delay += delay; 6918d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 6928d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 6938d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes} 6948d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 695be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::ThreadExitCallback(void* arg) { 696be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes Thread* self = reinterpret_cast<Thread*>(arg); 697be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self; 698b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 699b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 700be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::Startup() { 701b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro // Allocate a TLS slot. 7028d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_key_create, (&Thread::pthread_key_self_, Thread::ThreadExitCallback), "self key"); 703b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 704b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro // Double-check the TLS slot allocation. 705b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro if (pthread_getspecific(pthread_key_self_) != NULL) { 7063d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes LOG(FATAL) << "Newly-created pthread TLS slot is not NULL"; 707b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro } 708038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes} 709038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 7108e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 711ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic Class* FindClassOrDie(ClassLinker* class_linker, const char* descriptor) { 7128e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* c = class_linker->FindSystemClass(descriptor); 7138e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(c != NULL) << descriptor; 7148e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return c; 7158e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 7168e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 7178e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 718ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic Field* FindFieldOrDie(Class* c, const char* name, const char* descriptor) { 7196b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom Field* f = c->FindDeclaredInstanceField(name, descriptor); 7206b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom CHECK(f != NULL) << PrettyClass(c) << " " << name << " " << descriptor; 7218e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return f; 7228e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 7238e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 7248e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 725ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic Method* FindMethodOrDie(Class* c, const char* name, const char* signature) { 7268e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Method* m = c->FindVirtualMethod(name, signature); 7278e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(m != NULL) << PrettyClass(c) << " " << name << " " << signature; 7288e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return m; 7298e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 7308e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 731462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes// TODO: make more accessible? 732ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic Field* FindStaticFieldOrDie(Class* c, const char* name, const char* descriptor) { 733462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes Field* f = c->FindDeclaredStaticField(name, descriptor); 734462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes CHECK(f != NULL) << PrettyClass(c) << " " << name << " " << descriptor; 735462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes return f; 736462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes} 737462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes 738038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesvoid Thread::FinishStartup() { 7399a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson CHECK(Runtime::Current()->IsStarted()); 740b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom Thread* self = Thread::Current(); 741b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom 742b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom // Need to be kRunnable for FindClass 74334e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes ScopedThreadStateChange tsc(self, kRunnable); 744b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom 745038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes // Now the ClassLinker is ready, we can find the various Class*, Field*, and Method*s we need. 746038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 7478e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 7488e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* Thread_class = FindClassOrDie(class_linker, "Ljava/lang/Thread;"); 7498e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* UncaughtExceptionHandler_class = FindClassOrDie(class_linker, "Ljava/lang/Thread$UncaughtExceptionHandler;"); 750462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes gThreadGroup = FindClassOrDie(class_linker, "Ljava/lang/ThreadGroup;"); 7518e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThreadLock = FindClassOrDie(class_linker, "Ljava/lang/ThreadLock;"); 7528e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 7536b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_daemon = FindFieldOrDie(Thread_class, "daemon", "Z"); 7546b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_group = FindFieldOrDie(Thread_class, "group", "Ljava/lang/ThreadGroup;"); 7556b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_lock = FindFieldOrDie(Thread_class, "lock", "Ljava/lang/ThreadLock;"); 7566b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_name = FindFieldOrDie(Thread_class, "name", "Ljava/lang/String;"); 7576b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_priority = FindFieldOrDie(Thread_class, "priority", "I"); 7586b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_uncaughtHandler = FindFieldOrDie(Thread_class, "uncaughtHandler", "Ljava/lang/Thread$UncaughtExceptionHandler;"); 7596b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_vmData = FindFieldOrDie(Thread_class, "vmData", "I"); 760462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes gThreadGroup_name = FindFieldOrDie(gThreadGroup, "name", "Ljava/lang/String;"); 761462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes gThreadGroup_mMain = FindStaticFieldOrDie(gThreadGroup, "mMain", "Ljava/lang/ThreadGroup;"); 762462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes gThreadGroup_mSystem = FindStaticFieldOrDie(gThreadGroup, "mSystem", "Ljava/lang/ThreadGroup;"); 7636b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThreadLock_thread = FindFieldOrDie(gThreadLock, "thread", "Ljava/lang/Thread;"); 7648e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 7658e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_run = FindMethodOrDie(Thread_class, "run", "()V"); 766462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes gThreadGroup_removeThread = FindMethodOrDie(gThreadGroup, "removeThread", "(Ljava/lang/Thread;)V"); 7678e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gUncaughtExceptionHandler_uncaughtException = FindMethodOrDie(UncaughtExceptionHandler_class, 7688e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes "uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V"); 76901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 77001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Finish attaching the main thread. 771462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes Thread::Current()->CreatePeer("main", false, Thread::GetMainThreadGroup()); 7729a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson 7739a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson InitBoxingMethods(); 7749a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson class_linker->RunRootClinits(); 775b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 776b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 777c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughesvoid Thread::Shutdown() { 7788d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key"); 779c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes} 780c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes 7818e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesuint32_t Thread::LockOwnerFromThreadLock(Object* thread_lock) { 7828e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (thread_lock == NULL || thread_lock->GetClass() != gThreadLock) { 7838e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 7848e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 7858e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Object* managed_thread = gThreadLock_thread->GetObject(thread_lock); 7868e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (managed_thread == NULL) { 7878e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 7888e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 7898e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes uintptr_t vmData = static_cast<uintptr_t>(gThread_vmData->GetInt(managed_thread)); 7908e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Thread* thread = reinterpret_cast<Thread*>(vmData); 7918e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (thread == NULL) { 7928e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 7938e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 7948e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return thread->GetThinLockId(); 7958e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 7968e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 797dcc247493fd8fb243e335c3ec08e5e625896a47cElliott HughesThread::Thread() 79847179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes : thin_lock_id_(0), 79947179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes tid_(0), 80047179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes peer_(NULL), 8018e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes top_of_managed_stack_(), 8028e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes top_of_managed_stack_pc_(0), 803e62934d85fbc2d935afdad57eeade39ecbd7440aElliott Hughes wait_mutex_(new Mutex("a thread wait mutex")), 804e62934d85fbc2d935afdad57eeade39ecbd7440aElliott Hughes wait_cond_(new ConditionVariable("a thread wait condition variable")), 8058daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes wait_monitor_(NULL), 8068daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes interrupted_(false), 807dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes wait_next_(NULL), 8088e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes monitor_enter_object_(NULL), 809dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes card_table_(0), 8108daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes stack_end_(NULL), 811dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes native_to_managed_record_(NULL), 812dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes top_sirt_(NULL), 813f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien top_shadow_frame_(NULL), 814dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes jni_env_(NULL), 81534e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes state_(kNative), 816dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes self_(NULL), 817dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes runtime_(NULL), 818dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes exception_(NULL), 819dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes suspend_count_(0), 820234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes debug_suspend_count_(0), 82185d1545e985ac689db4bad7849880e843707c862Elliott Hughes class_loader_override_(NULL), 822418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes long_jump_context_(NULL), 823726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_(false), 824475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes pre_allocated_OutOfMemoryError_(NULL), 825e343b76af81a005ef64f5e75a555389fd9147dabjeffhao debug_invoke_req_(new DebugInvokeReq), 826899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes trace_stack_(new std::vector<TraceStackFrame>), 827899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes name_(new std::string("<native thread without managed peer>")) { 828f5a7a476e7ea63e094ff0f011dccc170607e6f6bElliott Hughes CHECK_EQ((sizeof(Thread) % 4), 0U) << sizeof(Thread); 829ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes memset(&held_mutexes_[0], 0, sizeof(held_mutexes_)); 830dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes} 831dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 832ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic void MonitorExitVisitor(const Object* object, void*) { 83302b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes Object* entered_monitor = const_cast<Object*>(object); 834ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughes LOG(WARNING) << "Calling MonitorExit on object " << object << " (" << PrettyTypeOf(object) << ")" 835ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughes << " left locked by native thread " << *Thread::Current() << " which is detaching"; 8365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes entered_monitor->MonitorExit(Thread::Current()); 83702b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes} 83802b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 839c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesvoid Thread::Destroy() { 84002b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited. 84193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes if (jni_env_ != NULL) { 84293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes jni_env_->monitors.VisitRoots(MonitorExitVisitor, NULL); 84393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes } 84402b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 84529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (peer_ != NULL) { 846c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes Thread* self = this; 84729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 848534da072d3db3beecbd74e5aa23d630d73695f2cElliott Hughes // We may need to call user-supplied managed code. 84934e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes SetState(kRunnable); 850534da072d3db3beecbd74e5aa23d630d73695f2cElliott Hughes 851534da072d3db3beecbd74e5aa23d630d73695f2cElliott Hughes HandleUncaughtExceptions(); 852534da072d3db3beecbd74e5aa23d630d73695f2cElliott Hughes RemoveFromThreadGroup(); 853534da072d3db3beecbd74e5aa23d630d73695f2cElliott Hughes 85429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // this.vmData = 0; 85593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes SetVmData(peer_, NULL); 85602b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 857c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes Dbg::PostThreadDeath(self); 85802b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 85929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // Thread.join() is implemented as an Object.wait() on the Thread.lock 86029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // object. Signal anyone who is waiting. 861038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes Object* lock = gThread_lock->GetObject(peer_); 862038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes // (This conditional is only needed for tests, where Thread.lock won't have been set.) 8635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (lock != NULL) { 8645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->MonitorEnter(self); 8655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->NotifyAll(); 8665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->MonitorExit(self); 8675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 8685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 869c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes} 87002b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 871c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott HughesThread::~Thread() { 872c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes delete jni_env_; 87302b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes jni_env_ = NULL; 87402b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 87534e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes SetState(kTerminated); 87685d1545e985ac689db4bad7849880e843707c862Elliott Hughes 87785d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete wait_cond_; 87885d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete wait_mutex_; 87985d1545e985ac689db4bad7849880e843707c862Elliott Hughes 880776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 88185d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete long_jump_context_; 882776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 883475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes 884475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes delete debug_invoke_req_; 885e343b76af81a005ef64f5e75a555389fd9147dabjeffhao delete trace_stack_; 886899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes delete name_; 887c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes} 888c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes 889accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughesvoid Thread::HandleUncaughtExceptions() { 890accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes if (!IsExceptionPending()) { 891accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes return; 892accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes } 893accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 894accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Get and clear the exception. 895accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Object* exception = GetException(); 896accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes ClearException(); 897accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 898accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // If the thread has its own handler, use that. 899accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Object* handler = gThread_uncaughtHandler->GetObject(peer_); 900accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes if (handler == NULL) { 901accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Otherwise use the thread group's default handler. 902a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes handler = GetThreadGroup(); 903accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes } 904accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 905accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Call the handler. 906accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Method* m = handler->GetClass()->FindVirtualMethodForVirtualOrInterface(gUncaughtExceptionHandler_uncaughtException); 90777405796564c6c1353807cda18b28678a719bd68Elliott Hughes JValue args[2]; 908f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes args[0].SetL(peer_); 909f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes args[1].SetL(exception); 91077405796564c6c1353807cda18b28678a719bd68Elliott Hughes m->Invoke(this, handler, args, NULL); 911accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 912accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // If the handler threw, clear that exception too. 913accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes ClearException(); 914accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 9154514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom 916a215526d5c789cbef0f81a1f9aba22541a841ccaElliott HughesObject* Thread::GetThreadGroup() const { 917a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes return gThread_group->GetObject(peer_); 918a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes} 919a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes 9204514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstromvoid Thread::RemoveFromThreadGroup() { 9214514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom // this.group.removeThread(this); 9224514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom // group can be null if we're in the compiler or a test. 923a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes Object* group = GetThreadGroup(); 9244514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom if (group != NULL) { 9254514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom Method* m = group->GetClass()->FindVirtualMethodForVirtualOrInterface(gThreadGroup_removeThread); 92677405796564c6c1353807cda18b28678a719bd68Elliott Hughes JValue args[1]; 927f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes args[0].SetL(peer_); 92877405796564c6c1353807cda18b28678a719bd68Elliott Hughes m->Invoke(this, group, args, NULL); 9294514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom } 9304514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom} 931accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 932408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogerssize_t Thread::NumSirtReferences() { 933a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers size_t count = 0; 93440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { 935a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers count += cur->NumberOfReferences(); 936a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 937a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return count; 938a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers} 939a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers 94028f1a147d77ec772db98bed890b50a9ddcff2365TDYasize_t Thread::NumShadowFrameReferences() { 94128f1a147d77ec772db98bed890b50a9ddcff2365TDYa size_t count = 0; 94228f1a147d77ec772db98bed890b50a9ddcff2365TDYa for (ShadowFrame* cur = top_shadow_frame_; cur; cur = cur->GetLink()) { 94328f1a147d77ec772db98bed890b50a9ddcff2365TDYa count += cur->NumberOfReferences(); 94428f1a147d77ec772db98bed890b50a9ddcff2365TDYa } 94528f1a147d77ec772db98bed890b50a9ddcff2365TDYa return count; 94628f1a147d77ec772db98bed890b50a9ddcff2365TDYa} 94728f1a147d77ec772db98bed890b50a9ddcff2365TDYa 948408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogersbool Thread::SirtContains(jobject obj) { 949408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers Object** sirt_entry = reinterpret_cast<Object**>(obj); 95040381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { 95140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom if (cur->Contains(sirt_entry)) { 952a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return true; 953a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 954a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 955a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return false; 956a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers} 957a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers 95828f1a147d77ec772db98bed890b50a9ddcff2365TDYabool Thread::ShadowFrameContains(jobject obj) { 95928f1a147d77ec772db98bed890b50a9ddcff2365TDYa Object** shadow_frame_entry = reinterpret_cast<Object**>(obj); 96028f1a147d77ec772db98bed890b50a9ddcff2365TDYa for (ShadowFrame* cur = top_shadow_frame_; cur; cur = cur->GetLink()) { 96128f1a147d77ec772db98bed890b50a9ddcff2365TDYa if (cur->Contains(shadow_frame_entry)) { 96228f1a147d77ec772db98bed890b50a9ddcff2365TDYa return true; 96328f1a147d77ec772db98bed890b50a9ddcff2365TDYa } 96428f1a147d77ec772db98bed890b50a9ddcff2365TDYa } 96528f1a147d77ec772db98bed890b50a9ddcff2365TDYa return false; 96628f1a147d77ec772db98bed890b50a9ddcff2365TDYa} 96728f1a147d77ec772db98bed890b50a9ddcff2365TDYa 96828f1a147d77ec772db98bed890b50a9ddcff2365TDYabool Thread::StackReferencesContain(jobject obj) { 96928f1a147d77ec772db98bed890b50a9ddcff2365TDYa return SirtContains(obj) || ShadowFrameContains(obj); 97028f1a147d77ec772db98bed890b50a9ddcff2365TDYa} 97128f1a147d77ec772db98bed890b50a9ddcff2365TDYa 9728dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liaovoid Thread::SirtVisitRoots(Heap::RootVisitor* visitor, void* arg) { 97340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { 9748dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao size_t num_refs = cur->NumberOfReferences(); 9758dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao for (size_t j = 0; j < num_refs; j++) { 97640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom Object* object = cur->GetReference(j); 9775e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom if (object != NULL) { 9785e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom visitor(object, arg); 9795e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom } 9808dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao } 9818dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao } 9828dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao} 9838dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 984f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chienvoid Thread::ShadowFrameVisitRoots(Heap::RootVisitor* visitor, void* arg) { 985f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien for (ShadowFrame* cur = top_shadow_frame_; cur; cur = cur->GetLink()) { 986f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien size_t num_refs = cur->NumberOfReferences(); 987f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien for (size_t j = 0; j < num_refs; j++) { 988f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien Object* object = cur->GetReference(j); 989f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien if (object != NULL) { 990f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien visitor(object, arg); 991f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien } 992f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien } 993f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien } 994f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien} 995f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien 996408f79aeb676251ba35667a64e86c20638d7cb0bIan RogersObject* Thread::DecodeJObject(jobject obj) { 9970cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers DCHECK(CanAccessDirectReferences()); 998408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (obj == NULL) { 999408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return NULL; 1000408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1001408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectRef ref = reinterpret_cast<IndirectRef>(obj); 1002408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectRefKind kind = GetIndirectRefKind(ref); 1003408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers Object* result; 1004408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers switch (kind) { 1005408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kLocal: 1006408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 100769f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughes IndirectReferenceTable& locals = jni_env_->locals; 1008cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(locals.Get(ref)); 1009408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 1010408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1011408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kGlobal: 1012408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 1013408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers JavaVMExt* vm = Runtime::Current()->GetJavaVM(); 1014408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectReferenceTable& globals = vm->globals; 1015408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers MutexLock mu(vm->globals_lock); 1016cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(globals.Get(ref)); 1017408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 1018408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1019408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kWeakGlobal: 1020408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 1021408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers JavaVMExt* vm = Runtime::Current()->GetJavaVM(); 1022408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectReferenceTable& weak_globals = vm->weak_globals; 1023408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers MutexLock mu(vm->weak_globals_lock); 1024cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(weak_globals.Get(ref)); 1025408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (result == kClearedJniWeakGlobal) { 1026408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // This is a special case where it's okay to return NULL. 1027408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return NULL; 1028408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1029408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 1030408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1031408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kSirtOrInvalid: 1032408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers default: 1033408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // TODO: make stack indirect reference table lookup more efficient 1034408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Check if this is a local reference in the SIRT 103528f1a147d77ec772db98bed890b50a9ddcff2365TDYa if (StackReferencesContain(obj)) { 10360cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers result = *reinterpret_cast<Object**>(obj); // Read from SIRT 1037c2dc62d1c9241dfe880a81698713c314b13aa63fElliott Hughes } else if (Runtime::Current()->GetJavaVM()->work_around_app_jni_bugs) { 1038408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Assume an invalid local reference is actually a direct pointer. 1039408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers result = reinterpret_cast<Object*>(obj); 1040408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } else { 1041a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes result = kInvalidIndirectRefObject; 1042408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1043408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1044408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 1045408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (result == NULL) { 1046a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes LOG(ERROR) << "JNI ERROR (app bug): use of deleted " << kind << ": " << obj; 1047a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes JniAbort(NULL); 1048a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes } else { 1049a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes if (result != kInvalidIndirectRefObject) { 1050b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes Runtime::Current()->GetHeap()->VerifyObject(result); 1051a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes } 1052408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1053408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return result; 1054408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers} 1055408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 10569b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liaoclass CountStackDepthVisitor : public Thread::StackVisitor { 10579b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 105829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes CountStackDepthVisitor() : depth_(0), skip_depth_(0), skipping_(true) {} 1059d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 10601bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes bool VisitFrame(const Frame& frame, uintptr_t /*pc*/) { 106129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // We want to skip frames up to and including the exception's constructor. 10629086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // Note we also skip the frame if it doesn't have a method (namely the callee 10639086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // save frame) 10645167c97a4f672ba821453418e3943988fabbfc43Ian Rogers if (skipping_ && frame.HasMethod() && 10655167c97a4f672ba821453418e3943988fabbfc43Ian Rogers !Throwable::GetJavaLangThrowable()->IsAssignableFrom(frame.GetMethod()->GetDeclaringClass())) { 106629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes skipping_ = false; 106729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 106829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (!skipping_) { 10696b0870dfaaa2e69a900ad7ed88e4e1d3697445aaIan Rogers if (frame.HasMethod()) { // ignore callee save frames 10706b0870dfaaa2e69a900ad7ed88e4e1d3697445aaIan Rogers ++depth_; 10716b0870dfaaa2e69a900ad7ed88e4e1d3697445aaIan Rogers } 107229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } else { 107329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes ++skip_depth_; 107429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 1075530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 10769b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 10779b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 10789b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao int GetDepth() const { 1079aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return depth_; 10809b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 10819b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 108229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int GetSkipDepth() const { 108329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes return skip_depth_; 108429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 108529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 10869b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao private: 1087aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t depth_; 108829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes uint32_t skip_depth_; 108929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes bool skipping_; 10909b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao}; 10919b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1092aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogersclass BuildInternalStackTraceVisitor : public Thread::StackVisitor { 10939b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 1094283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers explicit BuildInternalStackTraceVisitor(int skip_depth) 1095726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes : skip_depth_(skip_depth), count_(0), pc_trace_(NULL), method_trace_(NULL), local_ref_(NULL) { 1096283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers } 1097283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers 1098283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers bool Init(int depth, ScopedJniThreadState& ts) { 1099aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Allocate method trace with an extra slot that will hold the PC trace 110001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes method_trace_ = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(depth + 1); 1101726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (method_trace_ == NULL) { 1102283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers return false; 1103726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 1104aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Register a local reference as IntArray::Alloc may trigger GC 1105aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers local_ref_ = AddLocalReference<jobject>(ts.Env(), method_trace_); 1106aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers pc_trace_ = IntArray::Alloc(depth); 1107726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (pc_trace_ == NULL) { 1108283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers return false; 1109726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 1110aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#ifdef MOVING_GARBAGE_COLLECTOR 1111aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Re-read after potential GC 1112267364429db05d964c0922a8b4e49591c00bdc35Brian Carlstrom method_trace_ = Decode<ObjectArray<Object>*>(ts.Env(), local_ref_); 1113aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#endif 1114aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Save PC trace in last element of method trace, also places it into the 1115aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // object graph. 1116aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace_->Set(depth, pc_trace_); 1117283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers return true; 11189b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 11199b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1120aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers virtual ~BuildInternalStackTraceVisitor() {} 11219b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1122530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes bool VisitFrame(const Frame& frame, uintptr_t pc) { 1123726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (method_trace_ == NULL || pc_trace_ == NULL) { 1124530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; // We're probably trying to fillInStackTrace for an OutOfMemoryError. 1125726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 112629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (skip_depth_ > 0) { 112729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes skip_depth_--; 1128530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 112929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 11306b0870dfaaa2e69a900ad7ed88e4e1d3697445aaIan Rogers if (!frame.HasMethod()) { 1131530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; // ignore callee save frames 11326b0870dfaaa2e69a900ad7ed88e4e1d3697445aaIan Rogers } 1133aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace_->Set(count_, frame.GetMethod()); 1134bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers pc_trace_->Set(count_, pc); 1135aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ++count_; 1136530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 11379b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 11389b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1139aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers jobject GetInternalStackTrace() const { 1140aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return local_ref_; 11419b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 11429b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 11439b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao private: 114429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // How many more frames to skip. 114529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int32_t skip_depth_; 1146aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Current position down stack trace 1147aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t count_; 1148aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Array of return PC values 1149aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers IntArray* pc_trace_; 1150aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // An array of the methods on the stack, the last entry is a reference to the 1151aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // PC trace 1152aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ObjectArray<Object>* method_trace_; 1153aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Local indirect reference table entry for method trace 1154aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers jobject local_ref_; 11559b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao}; 11569b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1157776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 1158a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes// TODO: remove this. 1159ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic uintptr_t ManglePc(uintptr_t pc) { 1160a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // Move the PC back 2 bytes as a call will frequently terminate the 1161a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // decoding of a particular instruction and we want to make sure we 1162a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // get the Dex PC of the instruction with the call and not the 1163a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // instruction following. 1164a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes if (pc > 0) { pc -= 2; } 1165a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes return pc; 1166a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes} 1167776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 1168a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes 1169a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes// TODO: remove this. 1170ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic uintptr_t DemanglePc(uintptr_t pc) { 1171a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // Revert mangling for the case where we need the PC to return to the upcall 11729a8a8889418e09bf2d4ae3f683fbb40a71c3c8f4Ian Rogers if (pc > 0) { pc += 2; } 11739a8a8889418e09bf2d4ae3f683fbb40a71c3c8f4Ian Rogers return pc; 1174a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes} 1175f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers 1176f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chienvoid Thread::PushShadowFrame(ShadowFrame* frame) { 1177f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien frame->SetLink(top_shadow_frame_); 1178f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien top_shadow_frame_ = frame; 1179f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien} 1180f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien 1181f7ad17e108b9357d7c94c6218a8521140a667f3dLogan ChienShadowFrame* Thread::PopShadowFrame() { 1182f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien CHECK(top_shadow_frame_ != NULL); 1183f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien ShadowFrame* frame = top_shadow_frame_; 1184f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien top_shadow_frame_ = frame->GetLink(); 1185f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien return frame; 1186f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien} 1187f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien 118840381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstromvoid Thread::PushSirt(StackIndirectReferenceTable* sirt) { 118940381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom sirt->SetLink(top_sirt_); 119040381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom top_sirt_ = sirt; 119140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom} 119240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom 119340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian CarlstromStackIndirectReferenceTable* Thread::PopSirt() { 119440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom CHECK(top_sirt_ != NULL); 119540381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom StackIndirectReferenceTable* sirt = top_sirt_; 119640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom top_sirt_ = top_sirt_->GetLink(); 119740381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom return sirt; 119840381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom} 119940381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom 12009b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa#if !defined(ART_USE_LLVM_COMPILER) // LLVM use ShadowFrame 12019b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa 1202530fa005e2944d3b12712f80d974f0e753f568efElliott Hughesvoid Thread::WalkStack(StackVisitor* visitor, bool include_upcalls) const { 1203d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Frame frame = GetTopOfStack(); 1204a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes uintptr_t pc = ManglePc(top_of_managed_stack_pc_); 1205a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao uint32_t trace_stack_depth = 0; 12069b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao // TODO: enable this CHECK after native_to_managed_record_ is initialized during startup. 12079b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao // CHECK(native_to_managed_record_ != NULL); 12089b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao NativeToManagedRecord* record = native_to_managed_record_; 120957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers bool method_tracing_active = Runtime::Current()->IsMethodTracingActive(); 1210530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes while (frame.GetSP() != NULL) { 1211530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes for ( ; frame.GetMethod() != NULL; frame.Next()) { 1212f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom frame.GetMethod()->AssertPcIsWithinCode(pc); 1213530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes bool should_continue = visitor->VisitFrame(frame, pc); 121457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (UNLIKELY(!should_continue)) { 1215530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return; 1216530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } 121757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers uintptr_t return_pc = frame.GetReturnPC(); 121857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (LIKELY(!method_tracing_active)) { 121957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers pc = ManglePc(return_pc); 122057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } else { 12210791adc2249366c50684935a4c42ba5e58bc3746jeffhao // While profiling, the return pc is restored from the side stack, except when walking 12220791adc2249366c50684935a4c42ba5e58bc3746jeffhao // the stack for an exception where the side stack will be unwound in VisitFrame. 12230791adc2249366c50684935a4c42ba5e58bc3746jeffhao if (IsTraceExitPc(return_pc) && !include_upcalls) { 1224a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao TraceStackFrame trace_frame = GetTraceStackFrame(trace_stack_depth++); 122526c0a1a376d8cd639fe6b16594fcb55a723450c0jeffhao CHECK(trace_frame.method_ == frame.GetMethod()); 1226a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao pc = ManglePc(trace_frame.return_pc_); 122757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } else { 122857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers pc = ManglePc(return_pc); 1229a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao } 1230a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao } 12319b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 1232530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes if (include_upcalls) { 1233530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes bool should_continue = visitor->VisitFrame(frame, pc); 1234530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes if (!should_continue) { 1235530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return; 1236530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } 1237530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } 12389b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao if (record == NULL) { 1239530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return; 12409b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 1241bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // last_tos should return Frame instead of sp? 1242ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers frame.SetSP(reinterpret_cast<Method**>(record->last_top_of_managed_stack_)); 1243a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes pc = ManglePc(record->last_top_of_managed_stack_pc_); 1244bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers record = record->link_; 1245bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1246bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers} 1247bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 12489b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa#else // defined(ART_USE_LLVM_COMPILER) // LLVM uses ShadowFrame 12499b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa 12509b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYavoid Thread::WalkStack(StackVisitor* visitor, bool /*include_upcalls*/) const { 12519b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa for (ShadowFrame* cur = top_shadow_frame_; cur; cur = cur->GetLink()) { 12529b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa Frame frame; 12539b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa frame.SetSP(reinterpret_cast<Method**>(reinterpret_cast<byte*>(cur) + 12549b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa ShadowFrame::MethodOffset())); 125502f01fedc5180d1d5a795674f8c5bd0a3ce974d2Shih-wei Liao bool should_continue = visitor->VisitFrame(frame, cur->GetLineNumber()); 12569b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa if (!should_continue) { 12579b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa return; 12589b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa } 12599b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa } 12609b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa} 12619b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa 12629b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa/* 12639b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | | 12649b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | | 12659b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | | 12669b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | 12679b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | 12689b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | 12699b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | 12709b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | Method* | 12719b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | 12729b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | <-- top_shadow_frame_ (ShadowFrame*) 12739b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * / +------------------------+ 12749b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * ->| . | 12759b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * . | . | 12769b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * . | . | 12779b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * /+------------------------+ 12789b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * / | . | 12799b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * / | . | 12809b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * --- | | . | 12819b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | | | . | 12829b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | | Method* | <-- frame.GetSP() (Method**) 12839b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * ShadowFrame \ | . | 12849b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | ->| . | <-- cur (ShadowFrame*) 12859b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * --- /+------------------------+ 12869b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * / | . | 12879b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * / | . | 12889b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * --- | | . | 12899b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | cur->GetLink() | | . | 12909b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | | Method* | 12919b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * ShadowFrame \ | . | 12929b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | ->| . | 12939b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * --- +------------------------+ 12949b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | 12959b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | 12969b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | 12979b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * +========================+ 12989b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa */ 12999b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa 13009b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa#endif 13019b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa 130201158d7a57c8321370667a6045220237d16e0da8Elliott Hughesjobject Thread::CreateInternalStackTrace(JNIEnv* env) const { 1303aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Compute depth of stack 13049b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao CountStackDepthVisitor count_visitor; 13059b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao WalkStack(&count_visitor); 13069b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao int32_t depth = count_visitor.GetDepth(); 130729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int32_t skip_depth = count_visitor.GetSkipDepth(); 13084417536522fd2a9d8215d8672331984769c9520bShih-wei Liao 1309aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Transition into runnable state to work on Object*/Array* 131001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes ScopedJniThreadState ts(env); 1311aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 1312aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Build internal stack trace 131377f74c1455bd8d29deb8a9367cea35deb930ed10Ian Rogers BuildInternalStackTraceVisitor build_trace_visitor(skip_depth); 131477f74c1455bd8d29deb8a9367cea35deb930ed10Ian Rogers if (!build_trace_visitor.Init(depth, ts)) { 1315283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers return NULL; // Allocation failed 1316283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers } 1317267364429db05d964c0922a8b4e49591c00bdc35Brian Carlstrom WalkStack(&build_trace_visitor); 1318267364429db05d964c0922a8b4e49591c00bdc35Brian Carlstrom return build_trace_visitor.GetInternalStackTrace(); 1319aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers} 1320aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 132101158d7a57c8321370667a6045220237d16e0da8Elliott HughesjobjectArray Thread::InternalStackTraceToStackTraceElementArray(JNIEnv* env, jobject internal, 132201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobjectArray output_array, int* stack_depth) { 1323aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Transition into runnable state to work on Object*/Array* 1324aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ScopedJniThreadState ts(env); 1325aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Decode the internal stack trace into the depth, method trace and PC trace 1326aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ObjectArray<Object>* method_trace = 1327aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); 13289074b99bfbd5419e340f138a018b673ce71f77e8Ian Rogers int32_t depth = method_trace->GetLength() - 1; 1329aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); 1330aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 1331aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1332aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 133301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobjectArray result; 133401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes ObjectArray<StackTraceElement>* java_traces; 133501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes if (output_array != NULL) { 133601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Reuse the array we were given. 133701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes result = output_array; 133801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes java_traces = reinterpret_cast<ObjectArray<StackTraceElement>*>(Decode<Array*>(env, 133901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes output_array)); 134001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // ...adjusting the number of frames we'll write to not exceed the array length. 134101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes depth = std::min(depth, java_traces->GetLength()); 134201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } else { 134301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Create java_trace array and place in local reference table 134401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes java_traces = class_linker->AllocStackTraceElementArray(depth); 134530646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (java_traces == NULL) { 134630646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return NULL; 134730646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 134801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes result = AddLocalReference<jobjectArray>(ts.Env(), java_traces); 134901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } 135001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 135101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes if (stack_depth != NULL) { 135201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes *stack_depth = depth; 135301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } 135455df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 13556d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers MethodHelper mh; 13569b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao for (int32_t i = 0; i < depth; ++i) { 1357aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Prepare parameters for StackTraceElement(String cls, String method, String file, int line) 1358aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers Method* method = down_cast<Method*>(method_trace->Get(i)); 13596d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers mh.ChangeMethod(method); 1360aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t native_pc = pc_trace->Get(i); 136102f01fedc5180d1d5a795674f8c5bd0a3ce974d2Shih-wei Liao#if !defined(ART_USE_LLVM_COMPILER) 13626d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers int32_t line_number = mh.GetLineNumFromNativePC(native_pc); 136302f01fedc5180d1d5a795674f8c5bd0a3ce974d2Shih-wei Liao#else 136402f01fedc5180d1d5a795674f8c5bd0a3ce974d2Shih-wei Liao int32_t line_number = native_pc; // LLVM stored line_number in the ShadowFrame 136502f01fedc5180d1d5a795674f8c5bd0a3ce974d2Shih-wei Liao#endif 1366aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Allocate element, potentially triggering GC 136740381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom // TODO: reuse class_name_object via Class::name_? 1368486013193c94b4321414c28ce9251911e14a0599Ian Rogers const char* descriptor = mh.GetDeclaringClassDescriptor(); 1369486013193c94b4321414c28ce9251911e14a0599Ian Rogers CHECK(descriptor != NULL); 1370486013193c94b4321414c28ce9251911e14a0599Ian Rogers std::string class_name(PrettyDescriptor(descriptor)); 137140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom SirtRef<String> class_name_object(String::AllocFromModifiedUtf8(class_name.c_str())); 137240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom if (class_name_object.get() == NULL) { 137340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom return NULL; 137440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom } 1375486013193c94b4321414c28ce9251911e14a0599Ian Rogers const char* method_name = mh.GetName(); 1376486013193c94b4321414c28ce9251911e14a0599Ian Rogers CHECK(method_name != NULL); 1377486013193c94b4321414c28ce9251911e14a0599Ian Rogers SirtRef<String> method_name_object(String::AllocFromModifiedUtf8(method_name)); 13786d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers if (method_name_object.get() == NULL) { 13796d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers return NULL; 13806d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers } 1381486013193c94b4321414c28ce9251911e14a0599Ian Rogers const char* source_file = mh.GetDeclaringClassSourceFile(); 1382486013193c94b4321414c28ce9251911e14a0599Ian Rogers SirtRef<String> source_name_object(String::AllocFromModifiedUtf8(source_file)); 138340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom StackTraceElement* obj = StackTraceElement::Alloc(class_name_object.get(), 13846d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers method_name_object.get(), 13856d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers source_name_object.get(), 138640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom line_number); 138730646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (obj == NULL) { 138830646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return NULL; 138930646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1390aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#ifdef MOVING_GARBAGE_COLLECTOR 1391aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Re-read after potential GC 1392aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers java_traces = Decode<ObjectArray<Object>*>(ts.Env(), result); 1393aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace = down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); 1394aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); 1395aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#endif 139655df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao java_traces->Set(i, obj); 139755df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao } 1398aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return result; 139955df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao} 140055df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 14015cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughesvoid Thread::ThrowNewExceptionF(const char* exception_class_descriptor, const char* fmt, ...) { 1402a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_list args; 1403a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_start(args, fmt); 14044a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes ThrowNewExceptionV(exception_class_descriptor, fmt, args); 1405a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_end(args); 14064a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes} 14074a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes 14084a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughesvoid Thread::ThrowNewExceptionV(const char* exception_class_descriptor, const char* fmt, va_list ap) { 14094a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes std::string msg; 14104a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes StringAppendV(&msg, fmt, ap); 14115cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes ThrowNewException(exception_class_descriptor, msg.c_str()); 14125cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes} 141337f7a40f6789bb287f287a9af00777af9d6428eeElliott Hughes 14145cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughesvoid Thread::ThrowNewException(const char* exception_class_descriptor, const char* msg) { 1415e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes // Convert "Ljava/lang/Exception;" into JNI-style "java/lang/Exception". 14160cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers CHECK_EQ('L', exception_class_descriptor[0]); 1417e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes std::string descriptor(exception_class_descriptor + 1); 14180cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers CHECK_EQ(';', descriptor[descriptor.length() - 1]); 1419e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes descriptor.erase(descriptor.length() - 1); 1420e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes 1421e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes JNIEnv* env = GetJniEnv(); 1422726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jclass> exception_class(env, env->FindClass(descriptor.c_str())); 142330646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (exception_class.get() == NULL) { 142430646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI FindClass failed: " 142530646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes << PrettyTypeOf(GetException()); 142630646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes CHECK(IsExceptionPending()); 142730646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return; 142830646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1429ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom if (!Runtime::Current()->IsStarted()) { 1430ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom // Something is trying to throw an exception without a started 1431ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom // runtime, which is the common case in the compiler. We won't be 1432ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom // able to invoke the constructor of the exception, so use 1433ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom // AllocObject which will not invoke a constructor. 1434ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom ScopedLocalRef<jthrowable> exception( 1435ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom env, reinterpret_cast<jthrowable>(env->AllocObject(exception_class.get()))); 1436ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom if (exception.get() != NULL) { 1437ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom ScopedJniThreadState ts(env); 1438ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom Throwable* t = reinterpret_cast<Throwable*>(ts.Self()->DecodeJObject(exception.get())); 143902fbef0e356ca43b2032044f870d59de4a4ae92aIan Rogers t->SetDetailMessage(String::AllocFromModifiedUtf8(msg)); 1440ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom ts.Self()->SetException(t); 1441ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom } else { 1442ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI AllocObject failed: " 1443ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom << PrettyTypeOf(GetException()); 1444ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom CHECK(IsExceptionPending()); 1445ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom } 1446ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom return; 1447ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom } 1448726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes int rc = env->ThrowNew(exception_class.get(), msg); 144930646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (rc != JNI_OK) { 145030646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI ThrowNew failed: " 145130646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes << PrettyTypeOf(GetException()); 145230646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes CHECK(IsExceptionPending()); 145330646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1454a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes} 1455a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes 14562ced6a534157d5d963693346904389c19775d2daElliott Hughesvoid Thread::ThrowOutOfMemoryError(const char* msg) { 14572ced6a534157d5d963693346904389c19775d2daElliott Hughes LOG(ERROR) << StringPrintf("Throwing OutOfMemoryError \"%s\"%s", 14582ced6a534157d5d963693346904389c19775d2daElliott Hughes msg, (throwing_OutOfMemoryError_ ? " (recursive case)" : "")); 1459726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (!throwing_OutOfMemoryError_) { 1460726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_ = true; 1461accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes ThrowNewException("Ljava/lang/OutOfMemoryError;", NULL); 1462418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes } else { 1463726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes SetException(pre_allocated_OutOfMemoryError_); 1464418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes } 1465726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_ = false; 146679082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes} 146779082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes 1468accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1469498508c1187dc07d3eae5476784cde20f5224d93Elliott HughesThread* Thread::CurrentFromGdb() { 1470accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes return Thread::Current(); 1471accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 1472accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1473accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughesvoid Thread::DumpFromGdb() const { 14746b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom std::ostringstream ss; 14756b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom Dump(ss); 1476955724179c6c739524f610023287f56b24dc31deElliott Hughes std::string str(ss.str()); 14776b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom // log to stderr for debugging command line processes 14786b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom std::cerr << str; 14796b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom#ifdef HAVE_ANDROID_OS 14806b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom // log to logcat for debugging frameworks processes 14816b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom LOG(INFO) << str; 14826b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom#endif 1483accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 1484accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 148528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughesvoid Thread::DumpThreadOffset(std::ostream& os, uint32_t offset, size_t size_of_pointers) { 148628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes CHECK_EQ(size_of_pointers, 4U); // TODO: support 64-bit targets. 14873d71d0799748aac23ce5935d61b909bec6e96461Elliott Hughes#define DO_THREAD_OFFSET(x) if (offset == static_cast<uint32_t>(OFFSETOF_VOLATILE_MEMBER(Thread, x))) { os << # x; } else 148828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes#define DO_THREAD_ENTRY_POINT_OFFSET(x) if (offset == ENTRYPOINT_OFFSET(x)) { os << # x; } else 148928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(card_table_) 149028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(exception_) 149128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(jni_env_) 149228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(self_) 149328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(stack_end_) 14943d71d0799748aac23ce5935d61b909bec6e96461Elliott Hughes DO_THREAD_OFFSET(state_) 149528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(suspend_count_) 149628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(thin_lock_id_) 149728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(top_of_managed_stack_) 149828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(top_of_managed_stack_pc_) 149928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(top_sirt_) 150028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pAllocArrayFromCode) 150128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pAllocArrayFromCodeWithAccessCheck) 150228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pAllocObjectFromCode) 150328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pAllocObjectFromCodeWithAccessCheck) 150428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCheckAndAllocArrayFromCode) 150528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCheckAndAllocArrayFromCodeWithAccessCheck) 150628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInstanceofNonTrivialFromCode) 150728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCanPutArrayElementFromCode) 150828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCheckCastFromCode) 150928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pDebugMe) 151028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pUpdateDebuggerFromCode) 151128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInitializeStaticStorage) 151228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode) 151328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInitializeTypeFromCode) 151428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pResolveStringFromCode) 151528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pSet32Instance) 151628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pSet32Static) 151728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pSet64Instance) 151828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pSet64Static) 151928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pSetObjInstance) 152028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pSetObjStatic) 152128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pGet32Instance) 152228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pGet32Static) 152328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pGet64Instance) 152428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pGet64Static) 152528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pGetObjInstance) 152628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pGetObjStatic) 152728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pHandleFillArrayDataFromCode) 152828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pDecodeJObjectInThread) 152928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pFindNativeMethod) 153028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pLockObjectFromCode) 153128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pUnlockObjectFromCode) 153228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCmpgDouble) 153328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCmpgFloat) 153428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCmplDouble) 153528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCmplFloat) 153628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pDadd) 153728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pDdiv) 153828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pDmul) 153928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pDsub) 154028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pF2d) 154128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pFmod) 154228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pI2d) 154328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pL2d) 154428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pD2f) 154528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pFadd) 154628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pFdiv) 154728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pFmodf) 154828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pFmul) 154928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pFsub) 155028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pI2f) 155128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pL2f) 155228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pD2iz) 155328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pF2iz) 155428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pIdiv) 155528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pIdivmod) 155628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pD2l) 155728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pF2l) 155828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pLdiv) 155928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pLdivmod) 156028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pLmul) 156128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pShlLong) 156228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pShrLong) 156328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pUshrLong) 156428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pIndexOf) 156528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pMemcmp16) 156628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pStringCompareTo) 156728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pMemcpy) 156828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pFindInterfaceMethodInCache) 156928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pUnresolvedDirectMethodTrampolineFromCode) 157028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInvokeDirectTrampolineWithAccessCheck) 157128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInvokeInterfaceTrampoline) 157228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInvokeInterfaceTrampolineWithAccessCheck) 157328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInvokeStaticTrampolineWithAccessCheck) 157428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInvokeSuperTrampolineWithAccessCheck) 157528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInvokeVirtualTrampolineWithAccessCheck) 157628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCheckSuspendFromCode) 157728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pTestSuspendFromCode) 157828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pDeliverException) 157928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pThrowAbstractMethodErrorFromCode) 158028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pThrowArrayBoundsFromCode) 158128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pThrowDivZeroFromCode) 158228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pThrowNoSuchMethodFromCode) 158328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pThrowNullPointerFromCode) 158428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pThrowStackOverflowFromCode) 158528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pThrowVerificationErrorFromCode) 158628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes { 158728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes os << offset; 158828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes } 158928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes#undef DO_THREAD_OFFSET 159028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes#undef DO_THREAD_ENTRY_POINT_OFFSET 159128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes} 159228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes 1593bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogersclass CatchBlockStackVisitor : public Thread::StackVisitor { 1594bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers public: 1595bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers CatchBlockStackVisitor(Class* to_find, Context* ljc) 159657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers : to_find_(to_find), long_jump_context_(ljc), native_method_count_(0), 159757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers method_tracing_active_(Runtime::Current()->IsMethodTracingActive()) { 159867375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers#ifndef NDEBUG 159967375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_pc_ = 0xEBADC0DE; 160067375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_frame_.SetSP(reinterpret_cast<Method**>(0xEBADF00D)); 160167375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers#endif 160267375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers } 1603bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1604530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes bool VisitFrame(const Frame& fr, uintptr_t pc) { 1605530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes Method* method = fr.GetMethod(); 1606530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes if (method == NULL) { 1607530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes // This is the upcall, we remember the frame and last_pc so that we may 1608530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes // long jump to them 1609530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes handler_pc_ = DemanglePc(pc); 1610530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes handler_frame_ = fr; 161157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return false; // End stack walk. 1612530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } 1613530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes uint32_t dex_pc = DexFile::kDexNoIndex; 161457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (method->IsRuntimeMethod()) { 1615530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes // ignore callee save method 161657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers DCHECK(method->IsCalleeSaveMethod()); 1617530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } else if (method->IsNative()) { 1618530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes native_method_count_++; 1619530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } else { 16200791adc2249366c50684935a4c42ba5e58bc3746jeffhao // Unwind stack when an exception occurs during method tracing 162157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (UNLIKELY(method_tracing_active_)) { 1622776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 162357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (IsTraceExitPc(DemanglePc(pc))) { 1624530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes pc = ManglePc(TraceMethodUnwindFromCode(Thread::Current())); 1625530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } 1626776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#else 1627776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers UNIMPLEMENTED(FATAL); 1628776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 1629bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1630530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes dex_pc = method->ToDexPC(pc); 1631530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } 1632530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes if (dex_pc != DexFile::kDexNoIndex) { 1633530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes uint32_t found_dex_pc = method->FindCatchBlock(to_find_, dex_pc); 1634530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes if (found_dex_pc != DexFile::kDexNoIndex) { 1635530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes handler_pc_ = method->ToNativePC(found_dex_pc); 1636530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes handler_frame_ = fr; 163757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return false; // End stack walk. 1638bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 16391a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 1640776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 1641530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes // Caller may be handler, fill in callee saves in context 1642530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes long_jump_context_->FillCalleeSaves(fr); 1643776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 164457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return true; // Continue stack walk. 16451a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 16461a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 1647bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // The type of the exception catch block to find 1648bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Class* to_find_; 1649bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Frame with found handler or last frame if no handler found 1650bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Frame handler_frame_; 165167375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // PC to branch to for the handler 165267375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uintptr_t handler_pc_; 1653bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Context that will be the target of the long jump 1654bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Context* long_jump_context_; 165567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // Number of native methods passed in crawl (equates to number of SIRTs to pop) 165667375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uint32_t native_method_count_; 165757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers // Is method tracing active? 165857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers const bool method_tracing_active_; 1659bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers}; 1660bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1661ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogersvoid Thread::DeliverException() { 1662776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 166328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers const bool kDebugExceptionDelivery = false; 1664d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Throwable* exception = GetException(); // Get exception from thread 1665ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers CHECK(exception != NULL); 166628ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers // Don't leave exception visible while we try to find the handler, which may cause class 1667d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes // resolution. 166828ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers ClearException(); 166928ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (kDebugExceptionDelivery) { 1670a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers String* msg = exception->GetDetailMessage(); 1671a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers std::string str_msg(msg != NULL ? msg->ToModifiedUtf8() : ""); 1672a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception) 1673a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers << ": " << str_msg << std::endl); 167428ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 1675bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1676bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Context* long_jump_context = GetLongJumpContext(); 1677bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers CatchBlockStackVisitor catch_finder(exception->GetClass(), long_jump_context); 1678530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes WalkStack(&catch_finder, true); 1679bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1680d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Method** sp; 1681d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes uintptr_t throw_native_pc; 1682d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Method* throw_method = GetCurrentMethod(&throw_native_pc, &sp); 1683d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes uintptr_t catch_native_pc = catch_finder.handler_pc_; 1684d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Method* catch_method = catch_finder.handler_frame_.GetMethod(); 1685d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Dbg::PostException(sp, throw_method, throw_native_pc, catch_method, catch_native_pc, exception); 1686d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes 168728ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (kDebugExceptionDelivery) { 1688d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes if (catch_method == NULL) { 168928ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers LOG(INFO) << "Handler is upcall"; 169028ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } else { 169128ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 169228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers const DexFile& dex_file = 1693d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes class_linker->FindDexFile(catch_method->GetDeclaringClass()->GetDexCache()); 1694d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes int line_number = dex_file.GetLineNumFromPC(catch_method, 1695d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes catch_method->ToDexPC(catch_finder.handler_pc_)); 1696d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes LOG(INFO) << "Handler: " << PrettyMethod(catch_method) << " (line: " << line_number << ")"; 169728ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 169828ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 169928ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers SetException(exception); 17009a8a8889418e09bf2d4ae3f683fbb40a71c3c8f4Ian Rogers CHECK_NE(catch_native_pc, 0u); 1701d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes long_jump_context->SetSP(reinterpret_cast<uintptr_t>(catch_finder.handler_frame_.GetSP())); 17029a8a8889418e09bf2d4ae3f683fbb40a71c3c8f4Ian Rogers long_jump_context->SetPC(catch_native_pc); 17039c750f9b6283f62b3e6a93c0c6b2838abde5000eElliott Hughes long_jump_context->SmashCallerSaves(); 1704bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers long_jump_context->DoLongJump(); 1705776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 17069a8a8889418e09bf2d4ae3f683fbb40a71c3c8f4Ian Rogers LOG(FATAL) << "UNREACHABLE"; 1707bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers} 1708bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1709bdb0391258abc54bf77c676e36847d28a783bfe5Ian RogersContext* Thread::GetLongJumpContext() { 171085d1545e985ac689db4bad7849880e843707c862Elliott Hughes Context* result = long_jump_context_; 1711776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 1712bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (result == NULL) { 1713bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers result = Context::Create(); 171485d1545e985ac689db4bad7849880e843707c862Elliott Hughes long_jump_context_ = result; 17151a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 1716776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 1717bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers return result; 17181a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao} 17191a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 1720ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#if !defined(ART_USE_LLVM_COMPILER) 1721d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott HughesMethod* Thread::GetCurrentMethod(uintptr_t* pc, Method*** sp) const { 1722d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Frame f = top_of_managed_stack_; 1723d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Method* m = f.GetMethod(); 17248be2d40d42223b515de12629216151a558895969Elliott Hughes uintptr_t native_pc = top_of_managed_stack_pc_; 17258be2d40d42223b515de12629216151a558895969Elliott Hughes 17269fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // We use JNI internally for exception throwing, so it's possible to arrive 17279fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // here via a "FromCode" function, in which case there's a synthetic 17289fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // callee-save method at the top of the stack. These shouldn't be user-visible, 17299fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // so if we find one, skip it and return the compiled method underneath. 173033dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao if (m != NULL && m->IsCalleeSaveMethod()) { 17318be2d40d42223b515de12629216151a558895969Elliott Hughes native_pc = f.GetReturnPC(); 17329fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes f.Next(); 17339fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes m = f.GetMethod(); 17349fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes } 1735d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes if (pc != NULL) { 17368be2d40d42223b515de12629216151a558895969Elliott Hughes *pc = (m != NULL) ? ManglePc(native_pc) : 0; 173733dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao } 1738d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes if (sp != NULL) { 1739d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes *sp = f.GetSP(); 1740d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes } 1741d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes return m; 174233dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao} 1743ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#else 1744ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYaMethod* Thread::GetCurrentMethod(uintptr_t*, Method***) const { 1745ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa ShadowFrame* frame = top_shadow_frame_; 174620efe54063f2740d16f01190b280cdba384c869bTDYa if (frame == NULL) { 174720efe54063f2740d16f01190b280cdba384c869bTDYa return NULL; 1748ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa } 1749ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa return frame->GetMethod(); 1750ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa} 1751ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#endif 175233dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao 17535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesbool Thread::HoldsLock(Object* object) { 17545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (object == NULL) { 17555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 17565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 175724a3c2e9924e8765c4a9b4d383cb8f3b922f9c9fBrian Carlstrom return object->GetThinLockId() == thin_lock_id_; 17585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 17595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1760038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesbool Thread::IsDaemon() { 1761038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes return gThread_daemon->GetBoolean(peer_); 1762038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes} 1763038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 1764776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 1765d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogersclass ReferenceMapVisitor : public Thread::StackVisitor { 1766d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers public: 1767d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers ReferenceMapVisitor(Context* context, Heap::RootVisitor* root_visitor, void* arg) : 1768d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers context_(context), root_visitor_(root_visitor), arg_(arg) { 1769d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1770d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1771530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes bool VisitFrame(const Frame& frame, uintptr_t pc) { 1772d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Method* m = frame.GetMethod(); 17736a4be3a6226cec645cf905dd352e44f7968a7fa4Brian Carlstrom if (false) { 1774250455229aa0cc07bbd18174efe510bd52631a99jeffhao LOG(INFO) << "Visiting stack roots in " << PrettyMethod(m) 17750dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom << StringPrintf("@ PC:%04x", m->ToDexPC(pc)); 17766a4be3a6226cec645cf905dd352e44f7968a7fa4Brian Carlstrom } 1777d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Process register map (which native and callee save methods don't have) 17783891c775503b1113a918180d9ecdaa79e92c69c6Ian Rogers if (!m->IsNative() && !m->IsCalleeSaveMethod() && !m->IsProxyMethod()) { 1779e7d856b911222aa000ca2be0f8f63f5b292141c3Brian Carlstrom CHECK(m->GetGcMap() != NULL) << PrettyMethod(m); 1780e7d856b911222aa000ca2be0f8f63f5b292141c3Brian Carlstrom CHECK_NE(0U, m->GetGcMapLength()) << PrettyMethod(m); 1781e7d856b911222aa000ca2be0f8f63f5b292141c3Brian Carlstrom verifier::PcToReferenceMap map(m->GetGcMap(), m->GetGcMapLength()); 1782d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers const uint8_t* reg_bitmap = map.FindBitMap(m->ToDexPC(pc)); 1783d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers CHECK(reg_bitmap != NULL); 178468fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes const VmapTable vmap_table(m->GetVmapTableRaw()); 178511d1b0c31ddd710d26068da8e0e4621002205b4bElliott Hughes const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); 17866d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions? 17876d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers uint32_t core_spills = m->GetCoreSpillMask(); 17886d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers uint32_t fp_spills = m->GetFpSpillMask(); 17896d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers size_t frame_size = m->GetFrameSizeInBytes(); 17900dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom // For all dex registers in the bitmap 17916d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers size_t num_regs = std::min(map.RegWidth() * 8, 17926d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers static_cast<size_t>(code_item->registers_size_)); 17930dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom for (size_t reg = 0; reg < num_regs; ++reg) { 1794d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Does this register hold a reference? 1795d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers if (TestBitmap(reg, reg_bitmap)) { 179668fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes uint32_t vmap_offset; 1797d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Object* ref; 179868fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes if (vmap_table.IsInContext(reg, vmap_offset)) { 1799d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Compute the register we need to load from the context 1800d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers uint32_t spill_mask = m->GetCoreSpillMask(); 18010dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom CHECK_LT(vmap_offset, static_cast<uint32_t>(__builtin_popcount(spill_mask))); 1802f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers uint32_t matches = 0; 1803f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers uint32_t spill_shifts = 0; 1804f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers while (matches != (vmap_offset + 1)) { 18050dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom DCHECK_NE(spill_mask, 0u); 1806f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers matches += spill_mask & 1; // Add 1 if the low bit is set 1807f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_mask >>= 1; 1808f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_shifts++; 1809d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1810f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_shifts--; // wind back one as we want the last match 1811f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers ref = reinterpret_cast<Object*>(context_->GetGPR(spill_shifts)); 1812d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } else { 18136d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers ref = reinterpret_cast<Object*>(frame.GetVReg(code_item, core_spills, fp_spills, 18146d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers frame_size, reg)); 1815d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 18164f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao if (ref != NULL) { 18174f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao root_visitor_(ref, arg_); 18184f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao } 1819d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1820d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1821d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1822d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers context_->FillCalleeSaves(frame); 1823530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 1824d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1825d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1826d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers private: 1827d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers bool TestBitmap(int reg, const uint8_t* reg_vector) { 1828d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers return ((reg_vector[reg / 8] >> (reg % 8)) & 0x01) != 0; 1829d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1830d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1831d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Context used to build up picture of callee saves 1832d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Context* context_; 1833d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Call-back when we visit a root 1834d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Heap::RootVisitor* root_visitor_; 1835d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Argument to call-back 1836d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers void* arg_; 1837d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers}; 1838776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 1839d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1840d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogersvoid Thread::VisitRoots(Heap::RootVisitor* visitor, void* arg) { 1841d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (exception_ != NULL) { 1842d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes visitor(exception_, arg); 1843d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 1844d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (peer_ != NULL) { 1845d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes visitor(peer_, arg); 1846d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 1847726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (pre_allocated_OutOfMemoryError_ != NULL) { 1848726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes visitor(pre_allocated_OutOfMemoryError_, arg); 1849726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 185040381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom if (class_loader_override_ != NULL) { 185140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom visitor(class_loader_override_, arg); 185240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom } 1853410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes jni_env_->locals.VisitRoots(visitor, arg); 1854410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes jni_env_->monitors.VisitRoots(visitor, arg); 18558dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 18568dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao SirtVisitRoots(visitor, arg); 1857f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien ShadowFrameVisitRoots(visitor, arg); 18588dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 1859540a5b768bf5f76bd71ebf66612fb2298899480fTDYa#if !defined(ART_USE_LLVM_COMPILER) 1860d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Cheat and steal the long jump context. Assume that we are not doing a GC during exception 1861d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // delivery. 1862d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Context* context = GetLongJumpContext(); 1863d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Visit roots on this thread's stack 1864d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers ReferenceMapVisitor mapper(context, visitor, arg); 1865d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers WalkStack(&mapper); 1866540a5b768bf5f76bd71ebf66612fb2298899480fTDYa#endif 1867410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes} 1868410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes 1869250455229aa0cc07bbd18174efe510bd52631a99jeffhao#if VERIFY_OBJECT_ENABLED 1870ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic void VerifyObject(const Object* obj, void*) { 1871250455229aa0cc07bbd18174efe510bd52631a99jeffhao Runtime::Current()->GetHeap()->VerifyObject(obj); 1872250455229aa0cc07bbd18174efe510bd52631a99jeffhao} 1873250455229aa0cc07bbd18174efe510bd52631a99jeffhao 1874250455229aa0cc07bbd18174efe510bd52631a99jeffhaovoid Thread::VerifyStack() { 1875540a5b768bf5f76bd71ebf66612fb2298899480fTDYa#if !defined(ART_USE_LLVM_COMPILER) 1876e66ac79b90a12b01b9d67ee9f7f586e638e67fabjeffhao UniquePtr<Context> context(Context::Create()); 1877e66ac79b90a12b01b9d67ee9f7f586e638e67fabjeffhao ReferenceMapVisitor mapper(context.get(), VerifyObject, NULL); 1878250455229aa0cc07bbd18174efe510bd52631a99jeffhao WalkStack(&mapper); 1879540a5b768bf5f76bd71ebf66612fb2298899480fTDYa#endif 1880250455229aa0cc07bbd18174efe510bd52631a99jeffhao} 1881250455229aa0cc07bbd18174efe510bd52631a99jeffhao#endif 1882250455229aa0cc07bbd18174efe510bd52631a99jeffhao 1883330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughesstd::ostream& operator<<(std::ostream& os, const Thread& thread) { 1884899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes thread.Dump(os, false); 1885330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes return os; 1886330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes} 1887330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes 1888a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughesvoid Thread::CheckSafeToLockOrUnlock(MutexRank rank, bool is_locking) { 188917057b15cb8d8da97b2bc28fd38bdcc7a34e846eElliott Hughes if (this == NULL) { 189017057b15cb8d8da97b2bc28fd38bdcc7a34e846eElliott Hughes CHECK(Runtime::Current()->IsShuttingDown()); 189117057b15cb8d8da97b2bc28fd38bdcc7a34e846eElliott Hughes return; 189217057b15cb8d8da97b2bc28fd38bdcc7a34e846eElliott Hughes } 1893ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes if (is_locking) { 1894ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes if (held_mutexes_[rank] == 0) { 1895ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes bool bad_mutexes_held = false; 1896ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes for (int i = kMaxMutexRank; i > rank; --i) { 1897ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes if (held_mutexes_[i] != 0) { 1898ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes LOG(ERROR) << "holding " << static_cast<MutexRank>(i) << " while " << (is_locking ? "locking" : "unlocking") << " " << rank; 1899ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes bad_mutexes_held = true; 1900ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 1901ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 1902b5db3bb5b17f2bb42f3c62408938f4bf939f8a39Elliott Hughes CHECK(!bad_mutexes_held) << rank; 1903ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 1904ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes ++held_mutexes_[rank]; 1905ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } else { 1906b5db3bb5b17f2bb42f3c62408938f4bf939f8a39Elliott Hughes CHECK_GT(held_mutexes_[rank], 0U) << rank; 1907ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes --held_mutexes_[rank]; 1908ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 1909ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes} 1910ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes 1911a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughesvoid Thread::CheckSafeToWait(MutexRank rank) { 191217057b15cb8d8da97b2bc28fd38bdcc7a34e846eElliott Hughes if (this == NULL) { 191317057b15cb8d8da97b2bc28fd38bdcc7a34e846eElliott Hughes CHECK(Runtime::Current()->IsShuttingDown()); 191417057b15cb8d8da97b2bc28fd38bdcc7a34e846eElliott Hughes return; 191517057b15cb8d8da97b2bc28fd38bdcc7a34e846eElliott Hughes } 1916a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes bool bad_mutexes_held = false; 1917a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes for (int i = kMaxMutexRank; i >= 0; --i) { 1918a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes if (i != rank && held_mutexes_[i] != 0) { 1919a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes LOG(ERROR) << "holding " << static_cast<MutexRank>(i) << " while doing condition variable wait on " << rank; 1920a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes bad_mutexes_held = true; 1921a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes } 1922a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes } 1923a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes if (held_mutexes_[rank] == 0) { 1924a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes LOG(ERROR) << "*not* holding " << rank << " while doing condition variable wait on it"; 1925a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes bad_mutexes_held = true; 1926a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes } 1927a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes CHECK(!bad_mutexes_held); 1928a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes} 1929a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes 19308daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} // namespace art 1931