blob: b66fd1165562adacc2850752639623bb6216936e [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
26#include "object.h"
27#include "jdwp/jdwp.h"
28
29namespace art {
30
31struct Thread;
32
33/*
34 * Invoke-during-breakpoint support.
35 */
36struct DebugInvokeReq {
37 /* boolean; only set when we're in the tail end of an event handler */
38 bool ready;
39
40 /* boolean; set if the JDWP thread wants this thread to do work */
41 bool invokeNeeded;
42
43 /* request */
44 Object* obj; /* not used for ClassType.InvokeMethod */
45 Object* thread;
46 Class* class_;
47 Method* method;
48 uint32_t numArgs;
49 uint64_t* argArray; /* will be NULL if numArgs==0 */
50 uint32_t options;
51
52 /* result */
53 JDWP::JdwpError err;
54 uint8_t resultTag;
55 JValue resultValue;
56 JDWP::ObjectId exceptObj;
57
58 /* condition variable to wait on while the method executes */
59 Mutex lock_;
60 ConditionVariable cond_;
61};
62
63class Dbg {
64public:
65 static bool DebuggerStartup();
66 static void DebuggerShutdown();
67
68 // Return the DebugInvokeReq for the current thread.
69 static DebugInvokeReq* GetInvokeReq();
70
71 /*
72 * Enable/disable breakpoints and step modes. Used to provide a heads-up
73 * when the debugger attaches.
74 */
75 static void Connected();
76 static void Active();
77 static void Disconnected();
78
79 /*
80 * Returns "true" if a debugger is connected. Returns "false" if it's
81 * just DDM.
82 */
83 static bool IsDebuggerConnected();
84
85 static bool IsDebuggingEnabled();
86
87 /*
88 * Time, in milliseconds, since the last debugger activity. Does not
89 * include DDMS activity. Returns -1 if there has been no activity.
90 * Returns 0 if we're in the middle of handling a debugger request.
91 */
92 static int64_t LastDebuggerActivity();
93
94 /*
95 * Block/allow GC depending on what we're doing. These return the old
96 * status, which can be fed to ThreadContinuing() to restore the previous
97 * mode.
98 */
99 static int ThreadRunning();
100 static int ThreadWaiting();
101 static int ThreadContinuing(int status);
102
103 static void UndoDebuggerSuspensions();
104
105 // The debugger wants the VM to exit.
106 static void Exit(int status);
107
108 /*
109 * Class, Object, Array
110 */
111 static const char* GetClassDescriptor(JDWP::RefTypeId id);
112 static JDWP::ObjectId GetClassObject(JDWP::RefTypeId id);
113 static JDWP::RefTypeId GetSuperclass(JDWP::RefTypeId id);
114 static JDWP::ObjectId GetClassLoader(JDWP::RefTypeId id);
115 static uint32_t GetAccessFlags(JDWP::RefTypeId id);
116 static bool IsInterface(JDWP::RefTypeId id);
117 static void GetClassList(uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf);
118 static void GetVisibleClassList(JDWP::ObjectId classLoaderId, uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf);
119 static void GetClassInfo(JDWP::RefTypeId classId, uint8_t* pTypeTag, uint32_t* pStatus, const char** pSignature);
120 static bool FindLoadedClassBySignature(const char* classDescriptor, JDWP::RefTypeId* pRefTypeId);
121 static void GetObjectType(JDWP::ObjectId objectId, uint8_t* pRefTypeTag, JDWP::RefTypeId* pRefTypeId);
122 static uint8_t GetClassObjectType(JDWP::RefTypeId refTypeId);
123 static const char* GetSignature(JDWP::RefTypeId refTypeId);
124 static const char* GetSourceFile(JDWP::RefTypeId refTypeId);
125 static const char* GetObjectTypeName(JDWP::ObjectId objectId);
126 static uint8_t GetObjectTag(JDWP::ObjectId objectId);
127 static int GetTagWidth(int tag);
128
129 static int GetArrayLength(JDWP::ObjectId arrayId);
130 static uint8_t GetArrayElementTag(JDWP::ObjectId arrayId);
131 static bool OutputArray(JDWP::ObjectId arrayId, int firstIndex, int count, JDWP::ExpandBuf* pReply);
132 static bool SetArrayElements(JDWP::ObjectId arrayId, int firstIndex, int count, const uint8_t* buf);
133
134 static JDWP::ObjectId CreateString(const char* str);
135 static JDWP::ObjectId CreateObject(JDWP::RefTypeId classId);
136 static JDWP::ObjectId CreateArrayObject(JDWP::RefTypeId arrayTypeId, uint32_t length);
137
138 static bool MatchType(JDWP::RefTypeId instClassId, JDWP::RefTypeId classId);
139
140 /*
141 * Method and Field
142 */
143 static const char* GetMethodName(JDWP::RefTypeId refTypeId, JDWP::MethodId id);
144 static void OutputAllFields(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply);
145 static void OutputAllMethods(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply);
146 static void OutputAllInterfaces(JDWP::RefTypeId refTypeId, JDWP::ExpandBuf* pReply);
147 static void OutputLineTable(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId, JDWP::ExpandBuf* pReply);
148 static void OutputVariableTable(JDWP::RefTypeId refTypeId, JDWP::MethodId id, bool withGeneric, JDWP::ExpandBuf* pReply);
149
150 static uint8_t GetFieldBasicTag(JDWP::ObjectId objId, JDWP::FieldId fieldId);
151 static uint8_t GetStaticFieldBasicTag(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId);
152 static void GetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply);
153 static void SetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, uint64_t value, int width);
154 static void GetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply);
155 static void SetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, uint64_t rawValue, int width);
156
157 static char* StringToUtf8(JDWP::ObjectId strId);
158
159 /*
160 * Thread, ThreadGroup, Frame
161 */
162 static char* GetThreadName(JDWP::ObjectId threadId);
163 static JDWP::ObjectId GetThreadGroup(JDWP::ObjectId threadId);
164 static char* GetThreadGroupName(JDWP::ObjectId threadGroupId);
165 static JDWP::ObjectId GetThreadGroupParent(JDWP::ObjectId threadGroupId);
166 static JDWP::ObjectId GetSystemThreadGroupId();
167 static JDWP::ObjectId GetMainThreadGroupId();
168
169 static bool GetThreadStatus(JDWP::ObjectId threadId, uint32_t* threadStatus, uint32_t* suspendStatus);
170 static uint32_t GetThreadSuspendCount(JDWP::ObjectId threadId);
171 static bool ThreadExists(JDWP::ObjectId threadId);
172 static bool IsSuspended(JDWP::ObjectId threadId);
173 //static void WaitForSuspend(JDWP::ObjectId threadId);
174 static void GetThreadGroupThreads(JDWP::ObjectId threadGroupId, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount);
175 static void GetAllThreads(JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount);
176 static int GetThreadFrameCount(JDWP::ObjectId threadId);
177 static bool GetThreadFrame(JDWP::ObjectId threadId, int num, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc);
178
179 static JDWP::ObjectId GetThreadSelfId();
180 static void SuspendVM(bool isEvent);
181 static void ResumeVM();
182 static void SuspendThread(JDWP::ObjectId threadId);
183 static void ResumeThread(JDWP::ObjectId threadId);
184 static void SuspendSelf();
185
186 static bool GetThisObject(JDWP::ObjectId threadId, JDWP::FrameId frameId, JDWP::ObjectId* pThisId);
187 static void GetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint8_t* buf, int expectedLen);
188 static void SetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint64_t value, int width);
189
190 /*
191 * Debugger notification
192 */
193 enum {
194 kBreakPoint = 0x01,
195 kSingleStep = 0x02,
196 kMethodEntry = 0x04,
197 kMethodExit = 0x08,
198 };
199 static void PostLocationEvent(const Method* method, int pcOffset, Object* thisPtr, int eventFlags);
200 static void PostException(void* throwFp, int throwRelPc, void* catchFp, int catchRelPc, Object* exception);
201 static void PostThreadStart(Thread* t);
202 static void PostThreadDeath(Thread* t);
203 static void PostClassPrepare(Class* c);
204
205 static bool WatchLocation(const JDWP::JdwpLocation* pLoc);
206 static void UnwatchLocation(const JDWP::JdwpLocation* pLoc);
207 static bool ConfigureStep(JDWP::ObjectId threadId, JDWP::JdwpStepSize size, JDWP::JdwpStepDepth depth);
208 static void UnconfigureStep(JDWP::ObjectId threadId);
209
210 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);
211 static void ExecuteMethod(DebugInvokeReq* pReq);
212
213 /* perform "late registration" of an object ID */
214 static void RegisterObjectId(JDWP::ObjectId id);
215
216 /*
217 * DDM support.
218 */
219 static bool DdmHandlePacket(const uint8_t* buf, int dataLen, uint8_t** pReplyBuf, int* pReplyLen);
220 static void DdmConnected();
221 static void DdmDisconnected();
222 static void DdmSendChunk(int type, size_t len, const uint8_t* buf);
223 static void DdmSendChunkV(int type, const struct iovec* iov, int iovcnt);
224};
225
226#define CHUNK_TYPE(_name) \
227 ((_name)[0] << 24 | (_name)[1] << 16 | (_name)[2] << 8 | (_name)[3])
228
229} // namespace art
230
231#endif // ART_DEBUGGER_H_