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_callbacks.h"
32 #include "thread.h"
33 #include "thread_state.h"
34 
35 namespace art {
36 
37 class Dbg {
38  public:
39   static void SetJdwpAllowed(bool allowed);
40   static bool IsJdwpAllowed();
41 
42   // Invoked by the GC in case we need to keep DDMS informed.
43   static void GcDidFinish() REQUIRES(!Locks::mutator_lock_);
44 
45   static uint8_t ToJdwpThreadStatus(ThreadState state);
46 
47   // Indicates whether we need to force the use of interpreter when returning from the
48   // interpreter into the runtime. This allows to deoptimize the stack and continue
49   // execution with interpreter for debugging.
IsForcedInterpreterNeededForUpcall(Thread * thread,ArtMethod * m)50   static bool IsForcedInterpreterNeededForUpcall(Thread* thread, ArtMethod* m)
51       REQUIRES_SHARED(Locks::mutator_lock_) {
52     if (LIKELY(!thread->HasDebuggerShadowFrames())) {
53       return false;
54     }
55     // If we have debugger stack frames we always need to go back to interpreter unless we are
56     // native or a proxy.
57     return m != nullptr && !m->IsProxyMethod() && !m->IsNative();
58   }
59 
60   // Indicates whether we need to force the use of interpreter when handling an
61   // exception. This allows to deoptimize the stack and continue execution with
62   // the interpreter.
63   // Note: the interpreter will start by handling the exception when executing
64   // the deoptimized frames.
IsForcedInterpreterNeededForException(Thread * thread)65   static bool IsForcedInterpreterNeededForException(Thread* thread)
66       REQUIRES_SHARED(Locks::mutator_lock_) {
67     if (LIKELY(!thread->HasDebuggerShadowFrames())) {
68       return false;
69     }
70     return IsForcedInterpreterNeededForExceptionImpl(thread);
71   }
72 
73 
74   /*
75    * DDM support.
76    */
77   static void DdmSendThreadNotification(Thread* t, uint32_t type)
78       REQUIRES_SHARED(Locks::mutator_lock_);
79   static void DdmSetThreadNotification(bool enable)
80       REQUIRES(!Locks::thread_list_lock_);
81   static bool DdmHandleChunk(
82       JNIEnv* env,
83       uint32_t type,
84       const ArrayRef<const jbyte>& data,
85       /*out*/uint32_t* out_type,
86       /*out*/std::vector<uint8_t>* out_data);
87 
88   static void DdmConnected() REQUIRES_SHARED(Locks::mutator_lock_);
89   static void DdmDisconnected() REQUIRES_SHARED(Locks::mutator_lock_);
90 
91   /*
92    * Allocation tracking support.
93    */
94   static void SetAllocTrackingEnabled(bool enabled) REQUIRES(!Locks::alloc_tracker_lock_);
95   static jbyteArray GetRecentAllocations()
96       REQUIRES(!Locks::alloc_tracker_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
97   static void DumpRecentAllocations() REQUIRES(!Locks::alloc_tracker_lock_);
98 
99   enum HpifWhen {
100     HPIF_WHEN_NEVER = 0,
101     HPIF_WHEN_NOW = 1,
102     HPIF_WHEN_NEXT_GC = 2,
103     HPIF_WHEN_EVERY_GC = 3
104   };
105   static int DdmHandleHpifChunk(HpifWhen when)
106       REQUIRES_SHARED(Locks::mutator_lock_);
107 
108   enum HpsgWhen {
109     HPSG_WHEN_NEVER = 0,
110     HPSG_WHEN_EVERY_GC = 1,
111   };
112   enum HpsgWhat {
113     HPSG_WHAT_MERGED_OBJECTS = 0,
114     HPSG_WHAT_DISTINCT_OBJECTS = 1,
115   };
116   static bool DdmHandleHpsgNhsgChunk(HpsgWhen when, HpsgWhat what, bool native);
117 
118   static void DdmSendHeapInfo(HpifWhen reason)
119       REQUIRES_SHARED(Locks::mutator_lock_);
120   static void DdmSendHeapSegments(bool native)
121       REQUIRES_SHARED(Locks::mutator_lock_);
122 
GetThreadLifecycleCallback()123   static ThreadLifecycleCallback* GetThreadLifecycleCallback() {
124     return &thread_lifecycle_callback_;
125   }
126 
127  private:
128   static void DdmBroadcast(bool connect) REQUIRES_SHARED(Locks::mutator_lock_);
129 
130   static void PostThreadStart(Thread* t)
131       REQUIRES_SHARED(Locks::mutator_lock_);
132   static void PostThreadDeath(Thread* t)
133       REQUIRES_SHARED(Locks::mutator_lock_);
134   static void PostThreadStartOrStop(Thread*, uint32_t)
135       REQUIRES_SHARED(Locks::mutator_lock_);
136 
137   static bool IsForcedInterpreterNeededForExceptionImpl(Thread* thread)
138       REQUIRES_SHARED(Locks::mutator_lock_);
139 
140   class DbgThreadLifecycleCallback : public ThreadLifecycleCallback {
141    public:
142     void ThreadStart(Thread* self) override REQUIRES_SHARED(Locks::mutator_lock_);
143     void ThreadDeath(Thread* self) override REQUIRES_SHARED(Locks::mutator_lock_);
144   };
145 
146   static DbgThreadLifecycleCallback thread_lifecycle_callback_;
147 
148   DISALLOW_COPY_AND_ASSIGN(Dbg);
149 };
150 
151 #define CHUNK_TYPE(_name) \
152     static_cast<uint32_t>((_name)[0] << 24 | (_name)[1] << 16 | (_name)[2] << 8 | (_name)[3])
153 
154 }  // namespace art
155 
156 #endif  // ART_RUNTIME_DEBUGGER_H_
157