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