1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ART_RUNTIME_DEBUGGER_H_ 18 #define ART_RUNTIME_DEBUGGER_H_ 19 20 #include <pthread.h> 21 22 #include <set> 23 #include <string> 24 #include <vector> 25 26 #include "art_method.h" 27 #include "base/array_ref.h" 28 #include "base/locks.h" 29 #include "base/logging.h" 30 #include "jni.h" 31 #include "runtime.h" 32 #include "runtime_callbacks.h" 33 #include "thread.h" 34 #include "thread_state.h" 35 36 namespace art { 37 38 class Dbg { 39 public: 40 static void SetJdwpAllowed(bool allowed); 41 static bool IsJdwpAllowed(); 42 43 // Invoked by the GC in case we need to keep DDMS informed. 44 static void GcDidFinish() REQUIRES(!Locks::mutator_lock_); 45 46 static uint8_t ToJdwpThreadStatus(ThreadState state); 47 48 // Indicates whether we need to force the use of interpreter when returning from the 49 // interpreter into the runtime. This allows to deoptimize the stack and continue 50 // execution with interpreter for debugging. IsForcedInterpreterNeededForUpcall(Thread * thread,ArtMethod * m)51 static bool IsForcedInterpreterNeededForUpcall(Thread* thread, ArtMethod* m) 52 REQUIRES_SHARED(Locks::mutator_lock_) { 53 if (LIKELY(!thread->HasDebuggerShadowFrames())) { 54 return false; 55 } 56 // If we have debugger stack frames we always need to go back to interpreter unless we are 57 // native or a proxy. 58 return m != nullptr && !m->IsProxyMethod() && !m->IsNative(); 59 } 60 61 // Indicates whether we need to force the use of interpreter when handling an 62 // exception. This allows to deoptimize the stack and continue execution with 63 // the interpreter. 64 // Note: the interpreter will start by handling the exception when executing 65 // the deoptimized frames. IsForcedInterpreterNeededForException(Thread * thread)66 static bool IsForcedInterpreterNeededForException(Thread* thread) 67 REQUIRES_SHARED(Locks::mutator_lock_) { 68 // A quick check to avoid walking the stack. If there are no shadow frames or no method 69 // that needs to be deoptimized we can safely continue with optimized code. 70 if (LIKELY(!thread->HasDebuggerShadowFrames() && 71 Runtime::Current()->GetInstrumentation()->IsDeoptimizedMethodsEmpty())) { 72 return false; 73 } 74 return IsForcedInterpreterNeededForExceptionImpl(thread); 75 } 76 77 78 /* 79 * DDM support. 80 */ 81 static void DdmSendThreadNotification(Thread* t, uint32_t type) 82 REQUIRES_SHARED(Locks::mutator_lock_); 83 static void DdmSetThreadNotification(bool enable) 84 REQUIRES(!Locks::thread_list_lock_); 85 static bool DdmHandleChunk( 86 JNIEnv* env, 87 uint32_t type, 88 const ArrayRef<const jbyte>& data, 89 /*out*/uint32_t* out_type, 90 /*out*/std::vector<uint8_t>* out_data); 91 92 static void DdmConnected() REQUIRES_SHARED(Locks::mutator_lock_); 93 static void DdmDisconnected() REQUIRES_SHARED(Locks::mutator_lock_); 94 95 /* 96 * Allocation tracking support. 97 */ 98 static void SetAllocTrackingEnabled(bool enabled) REQUIRES(!Locks::alloc_tracker_lock_); 99 static jbyteArray GetRecentAllocations() 100 REQUIRES(!Locks::alloc_tracker_lock_) REQUIRES_SHARED(Locks::mutator_lock_); 101 static void DumpRecentAllocations() REQUIRES(!Locks::alloc_tracker_lock_); 102 103 enum HpifWhen { 104 HPIF_WHEN_NEVER = 0, 105 HPIF_WHEN_NOW = 1, 106 HPIF_WHEN_NEXT_GC = 2, 107 HPIF_WHEN_EVERY_GC = 3 108 }; 109 static int DdmHandleHpifChunk(HpifWhen when) 110 REQUIRES_SHARED(Locks::mutator_lock_); 111 112 enum HpsgWhen { 113 HPSG_WHEN_NEVER = 0, 114 HPSG_WHEN_EVERY_GC = 1, 115 }; 116 enum HpsgWhat { 117 HPSG_WHAT_MERGED_OBJECTS = 0, 118 HPSG_WHAT_DISTINCT_OBJECTS = 1, 119 }; 120 static bool DdmHandleHpsgNhsgChunk(HpsgWhen when, HpsgWhat what, bool native); 121 122 static void DdmSendHeapInfo(HpifWhen reason) 123 REQUIRES_SHARED(Locks::mutator_lock_); 124 static void DdmSendHeapSegments(bool native) 125 REQUIRES_SHARED(Locks::mutator_lock_); 126 GetThreadLifecycleCallback()127 static ThreadLifecycleCallback* GetThreadLifecycleCallback() { 128 return &thread_lifecycle_callback_; 129 } 130 131 private: 132 static void DdmBroadcast(bool connect) REQUIRES_SHARED(Locks::mutator_lock_); 133 134 static void PostThreadStart(Thread* t) 135 REQUIRES_SHARED(Locks::mutator_lock_); 136 static void PostThreadDeath(Thread* t) 137 REQUIRES_SHARED(Locks::mutator_lock_); 138 static void PostThreadStartOrStop(Thread*, uint32_t) 139 REQUIRES_SHARED(Locks::mutator_lock_); 140 141 static bool IsForcedInterpreterNeededForExceptionImpl(Thread* thread) 142 REQUIRES_SHARED(Locks::mutator_lock_); 143 144 class DbgThreadLifecycleCallback : public ThreadLifecycleCallback { 145 public: 146 void ThreadStart(Thread* self) override REQUIRES_SHARED(Locks::mutator_lock_); 147 void ThreadDeath(Thread* self) override REQUIRES_SHARED(Locks::mutator_lock_); 148 }; 149 150 static DbgThreadLifecycleCallback thread_lifecycle_callback_; 151 152 DISALLOW_COPY_AND_ASSIGN(Dbg); 153 }; 154 155 #define CHUNK_TYPE(_name) \ 156 static_cast<uint32_t>((_name)[0] << 24 | (_name)[1] << 16 | (_name)[2] << 8 | (_name)[3]) 157 158 } // namespace art 159 160 #endif // ART_RUNTIME_DEBUGGER_H_ 161