blob: b85a65d126e21c7efd6075958977c88726e080bf [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
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/*
18 * Dalvik-specific side of debugger support. (The JDWP code is intended to
19 * be relatively generic.)
20 */
21#ifndef ART_DEBUGGER_H_
22#define ART_DEBUGGER_H_
23
24#include <pthread.h>
25
Elliott Hughes3bb81562011-10-21 18:52:59 -070026#include <string>
27
Elliott Hughes872d4ec2011-10-21 17:07:15 -070028#include "jdwp/jdwp.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070029#include "object.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070030
31namespace art {
32
33struct Thread;
34
35/*
36 * Invoke-during-breakpoint support.
37 */
38struct DebugInvokeReq {
Elliott Hughes475fc232011-10-25 15:00:35 -070039 DebugInvokeReq() : lock_("a DebugInvokeReq lock"), cond_("a DebugInvokeReq condition variable") {
40 }
41
Elliott Hughes872d4ec2011-10-21 17:07:15 -070042 /* boolean; only set when we're in the tail end of an event handler */
43 bool ready;
44
45 /* boolean; set if the JDWP thread wants this thread to do work */
Elliott Hughes475fc232011-10-25 15:00:35 -070046 bool invoke_needed;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070047
48 /* request */
49 Object* obj; /* not used for ClassType.InvokeMethod */
50 Object* thread;
51 Class* class_;
52 Method* method;
Elliott Hughes475fc232011-10-25 15:00:35 -070053 uint32_t num_args;
54 uint64_t* arg_array; /* will be NULL if numArgs==0 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -070055 uint32_t options;
56
57 /* result */
Elliott Hughes475fc232011-10-25 15:00:35 -070058 JDWP::JdwpError error;
59 uint8_t result_tag;
60 JValue result_value;
61 JDWP::ObjectId exception;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070062
63 /* condition variable to wait on while the method executes */
64 Mutex lock_;
65 ConditionVariable cond_;
66};
67
68class Dbg {
69public:
Elliott Hughes3bb81562011-10-21 18:52:59 -070070 static bool ParseJdwpOptions(const std::string& options);
Elliott Hughes4ffd3132011-10-24 12:06:42 -070071 static void SetJdwpAllowed(bool allowed);
72
Elliott Hughesd1cc8362011-10-24 16:58:50 -070073 static void StartJdwp();
74 static void StopJdwp();
75
Elliott Hughes767a1472011-10-26 18:49:02 -070076 // Invoked by the GC in case we need to keep DDMS informed.
77 static void GcDidFinish();
78
Elliott Hughes872d4ec2011-10-21 17:07:15 -070079 // Return the DebugInvokeReq for the current thread.
80 static DebugInvokeReq* GetInvokeReq();
81
Elliott Hughes475fc232011-10-25 15:00:35 -070082 static Thread* GetDebugThread();
83 static void ClearWaitForEventThread();
84
Elliott Hughes872d4ec2011-10-21 17:07:15 -070085 /*
86 * Enable/disable breakpoints and step modes. Used to provide a heads-up
87 * when the debugger attaches.
88 */
89 static void Connected();
90 static void Active();
91 static void Disconnected();
92
93 /*
94 * Returns "true" if a debugger is connected. Returns "false" if it's
95 * just DDM.
96 */
97 static bool IsDebuggerConnected();
98
99 static bool IsDebuggingEnabled();
100
101 /*
102 * Time, in milliseconds, since the last debugger activity. Does not
103 * include DDMS activity. Returns -1 if there has been no activity.
104 * Returns 0 if we're in the middle of handling a debugger request.
105 */
106 static int64_t LastDebuggerActivity();
107
108 /*
109 * Block/allow GC depending on what we're doing. These return the old
110 * status, which can be fed to ThreadContinuing() to restore the previous
111 * mode.
112 */
113 static int ThreadRunning();
114 static int ThreadWaiting();
115 static int ThreadContinuing(int status);
116
117 static void UndoDebuggerSuspensions();
118
119 // The debugger wants the VM to exit.
120 static void Exit(int status);
121
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700122 static void VisitRoots(Heap::RootVisitor* visitor, void* arg);
123
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700124 /*
125 * Class, Object, Array
126 */
127 static const char* GetClassDescriptor(JDWP::RefTypeId id);
128 static JDWP::ObjectId GetClassObject(JDWP::RefTypeId id);
129 static JDWP::RefTypeId GetSuperclass(JDWP::RefTypeId id);
130 static JDWP::ObjectId GetClassLoader(JDWP::RefTypeId id);
131 static uint32_t GetAccessFlags(JDWP::RefTypeId id);
132 static bool IsInterface(JDWP::RefTypeId id);
133 static void GetClassList(uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf);
134 static void GetVisibleClassList(JDWP::ObjectId classLoaderId, uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf);
135 static void GetClassInfo(JDWP::RefTypeId classId, uint8_t* pTypeTag, uint32_t* pStatus, const char** pSignature);
136 static bool FindLoadedClassBySignature(const char* classDescriptor, JDWP::RefTypeId* pRefTypeId);
137 static void GetObjectType(JDWP::ObjectId objectId, uint8_t* pRefTypeTag, JDWP::RefTypeId* pRefTypeId);
138 static uint8_t GetClassObjectType(JDWP::RefTypeId refTypeId);
139 static const char* GetSignature(JDWP::RefTypeId refTypeId);
140 static const char* GetSourceFile(JDWP::RefTypeId refTypeId);
141 static const char* GetObjectTypeName(JDWP::ObjectId objectId);
142 static uint8_t GetObjectTag(JDWP::ObjectId objectId);
143 static int GetTagWidth(int tag);
144
145 static int GetArrayLength(JDWP::ObjectId arrayId);
146 static uint8_t GetArrayElementTag(JDWP::ObjectId arrayId);
147 static bool OutputArray(JDWP::ObjectId arrayId, int firstIndex, int count, JDWP::ExpandBuf* pReply);
148 static bool SetArrayElements(JDWP::ObjectId arrayId, int firstIndex, int count, const uint8_t* buf);
149
150 static JDWP::ObjectId CreateString(const char* str);
151 static JDWP::ObjectId CreateObject(JDWP::RefTypeId classId);
152 static JDWP::ObjectId CreateArrayObject(JDWP::RefTypeId arrayTypeId, uint32_t length);
153
154 static bool MatchType(JDWP::RefTypeId instClassId, JDWP::RefTypeId classId);
155
156 /*
157 * Method and Field
158 */
159 static const char* GetMethodName(JDWP::RefTypeId refTypeId, JDWP::MethodId id);
160 static void OutputAllFields(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply);
161 static void OutputAllMethods(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply);
162 static void OutputAllInterfaces(JDWP::RefTypeId refTypeId, JDWP::ExpandBuf* pReply);
163 static void OutputLineTable(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId, JDWP::ExpandBuf* pReply);
164 static void OutputVariableTable(JDWP::RefTypeId refTypeId, JDWP::MethodId id, bool withGeneric, JDWP::ExpandBuf* pReply);
165
166 static uint8_t GetFieldBasicTag(JDWP::ObjectId objId, JDWP::FieldId fieldId);
167 static uint8_t GetStaticFieldBasicTag(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId);
168 static void GetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply);
169 static void SetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, uint64_t value, int width);
170 static void GetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply);
171 static void SetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, uint64_t rawValue, int width);
172
173 static char* StringToUtf8(JDWP::ObjectId strId);
174
175 /*
176 * Thread, ThreadGroup, Frame
177 */
178 static char* GetThreadName(JDWP::ObjectId threadId);
179 static JDWP::ObjectId GetThreadGroup(JDWP::ObjectId threadId);
180 static char* GetThreadGroupName(JDWP::ObjectId threadGroupId);
181 static JDWP::ObjectId GetThreadGroupParent(JDWP::ObjectId threadGroupId);
182 static JDWP::ObjectId GetSystemThreadGroupId();
183 static JDWP::ObjectId GetMainThreadGroupId();
184
185 static bool GetThreadStatus(JDWP::ObjectId threadId, uint32_t* threadStatus, uint32_t* suspendStatus);
186 static uint32_t GetThreadSuspendCount(JDWP::ObjectId threadId);
187 static bool ThreadExists(JDWP::ObjectId threadId);
188 static bool IsSuspended(JDWP::ObjectId threadId);
189 //static void WaitForSuspend(JDWP::ObjectId threadId);
190 static void GetThreadGroupThreads(JDWP::ObjectId threadGroupId, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount);
191 static void GetAllThreads(JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount);
192 static int GetThreadFrameCount(JDWP::ObjectId threadId);
193 static bool GetThreadFrame(JDWP::ObjectId threadId, int num, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc);
194
195 static JDWP::ObjectId GetThreadSelfId();
Elliott Hughes475fc232011-10-25 15:00:35 -0700196 static void SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700197 static void ResumeVM();
198 static void SuspendThread(JDWP::ObjectId threadId);
199 static void ResumeThread(JDWP::ObjectId threadId);
200 static void SuspendSelf();
201
202 static bool GetThisObject(JDWP::ObjectId threadId, JDWP::FrameId frameId, JDWP::ObjectId* pThisId);
203 static void GetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint8_t* buf, int expectedLen);
204 static void SetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint64_t value, int width);
205
206 /*
207 * Debugger notification
208 */
209 enum {
210 kBreakPoint = 0x01,
211 kSingleStep = 0x02,
212 kMethodEntry = 0x04,
213 kMethodExit = 0x08,
214 };
215 static void PostLocationEvent(const Method* method, int pcOffset, Object* thisPtr, int eventFlags);
216 static void PostException(void* throwFp, int throwRelPc, void* catchFp, int catchRelPc, Object* exception);
217 static void PostThreadStart(Thread* t);
218 static void PostThreadDeath(Thread* t);
219 static void PostClassPrepare(Class* c);
220
221 static bool WatchLocation(const JDWP::JdwpLocation* pLoc);
222 static void UnwatchLocation(const JDWP::JdwpLocation* pLoc);
223 static bool ConfigureStep(JDWP::ObjectId threadId, JDWP::JdwpStepSize size, JDWP::JdwpStepDepth depth);
224 static void UnconfigureStep(JDWP::ObjectId threadId);
225
226 static JDWP::JdwpError InvokeMethod(JDWP::ObjectId threadId, JDWP::ObjectId objectId, JDWP::RefTypeId classId, JDWP::MethodId methodId, uint32_t numArgs, uint64_t* argArray, uint32_t options, uint8_t* pResultTag, uint64_t* pResultValue, JDWP::ObjectId* pExceptObj);
227 static void ExecuteMethod(DebugInvokeReq* pReq);
228
229 /* perform "late registration" of an object ID */
230 static void RegisterObjectId(JDWP::ObjectId id);
231
232 /*
233 * DDM support.
234 */
Elliott Hughes47fce012011-10-25 18:37:19 -0700235 static void DdmSetThreadNotification(bool enable);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700236 static bool DdmHandlePacket(const uint8_t* buf, int dataLen, uint8_t** pReplyBuf, int* pReplyLen);
237 static void DdmConnected();
238 static void DdmDisconnected();
239 static void DdmSendChunk(int type, size_t len, const uint8_t* buf);
240 static void DdmSendChunkV(int type, const struct iovec* iov, int iovcnt);
Elliott Hughes767a1472011-10-26 18:49:02 -0700241
242 enum HpifWhen {
243 HPIF_WHEN_NEVER = 0,
244 HPIF_WHEN_NOW = 1,
245 HPIF_WHEN_NEXT_GC = 2,
246 HPIF_WHEN_EVERY_GC = 3
247 };
248 static int DdmHandleHpifChunk(HpifWhen when);
249
250 enum HpsgWhen {
251 HPSG_WHEN_NEVER = 0,
252 HPSG_WHEN_EVERY_GC = 1,
253 };
254 enum HpsgWhat {
255 HPSG_WHAT_MERGED_OBJECTS = 0,
256 HPSG_WHAT_DISTINCT_OBJECTS = 1,
257 };
258 static bool DdmHandleHpsgNhsgChunk(HpsgWhen when, HpsgWhat what, bool native);
259
Elliott Hughes7162ad92011-10-27 14:08:42 -0700260 static void DdmSendHeapInfo(HpifWhen reason);
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700261 static void DdmSendHeapSegments(bool native);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700262};
263
264#define CHUNK_TYPE(_name) \
265 ((_name)[0] << 24 | (_name)[1] << 16 | (_name)[2] << 8 | (_name)[3])
266
267} // namespace art
268
269#endif // ART_DEBUGGER_H_